/******************************************************************************
 * * mmi_modem_adaptor_sim.c - implementation of mmi-modem adaptor, sim sub-module
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#include "lv_watch.h"

#if USE_LV_WATCH_MODEM_ADAPTOR
#include "mmi_modem_adaptor_sim.h"
#include "mmi_modem_adaptor_call.h"
Modem_SIM_Mng_t g_ModemSimMng;

/******************************************************************************
 *
 *         Functions
 *
 *******************************************************************************/

/**
 * initiate mmi-modem adaptor module
 * param (in) void:
 * return  void
 */
VOID MMI_ModemAdp_Sim_Init_Req()
{
    printf("##interface## %s\n", __FUNCTION__);

    g_ModemSimMng.SimPresent[0]         = FALSE;
    g_ModemSimMng.SimPresent[1]         = FALSE;
    g_ModemSimMng.SimOperationStatus[0] = MODEMADP_SIM_IDLE;
    g_ModemSimMng.SimOperationStatus[1] = MODEMADP_SIM_IDLE;
    g_ModemSimMng.WritePbContact[0]     = NULL;
    g_ModemSimMng.WritePbContact[1]     = NULL;
    g_ModemSimMng.ReadPbContact[0]      = NULL;
    g_ModemSimMng.ReadPbContact[1]      = NULL;
    g_ModemSimMng.ContactsUsed[0]       = 0;
    g_ModemSimMng.ContactsUsed[1]       = 0;
    g_ModemSimMng.Imsi[MMI_MODEM_SIM_1] = NULL;
    g_ModemSimMng.Imsi[MMI_MODEM_SIM_2] = NULL;
    Hal_Mem_Set(&g_ModemSimMng.RegHomePlmn[MMI_MODEM_SIM_1], 0, sizeof(MODEMADP_SIM_REG_AND_HOME_PLMN_INFO));
    Hal_Mem_Set(&g_ModemSimMng.RegHomePlmn[MMI_MODEM_SIM_2], 0, sizeof(MODEMADP_SIM_REG_AND_HOME_PLMN_INFO));
    g_ModemSimMng.Iccid[MMI_MODEM_SIM_1] = NULL;
    g_ModemSimMng.Msisdn[MMI_MODEM_SIM_1] = NULL;
    g_ModemSimMng.Iccid[MMI_MODEM_SIM_2] = NULL;
    g_ModemSimMng.Msisdn[MMI_MODEM_SIM_2] = NULL;
}

/******************************************************************************
 *
 *         MMI -> Modem Adaptor
 *
 *******************************************************************************/

/**
 * query facility PIN lock info
 * param (in) PinLockSet:  MMI_Modem_Pin_Lock_t*
 * return  void
 */
VOID  MMI_Modem_Query_Facility_Pin_Lock(MMI_Modem_Pin_Lock_t * PinLockSet)
{
    MMI_Modemadp_Query_Facility_Pin_Lock_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Query_Facility_Pin_Lock_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Query_Facility_Pin_Lock_t));

    g_ModemSimMng.PinCheckSema = Hal_Sema_Create(0);
    pMsg->PinLockSet           = PinLockSet;
    g_ModemSimMng.PinCheckRetryLeft = 0;
    g_ModemSimMng.QueryFacPinLockResult = 0;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_QUERY_FACILITY_PIN_LOCK, (UI_Inter_Msg_t *)pMsg);
    Hal_Sema_Lock(g_ModemSimMng.PinCheckSema);
    Hal_Sema_Destroy(g_ModemSimMng.PinCheckSema);

    // wait modem adaptor update the retried count and unlock semphore
    PinLockSet->PinEnable = g_ModemSimMng.QueryFacPinLockResult;
    PinLockSet->RetriesLeft = g_ModemSimMng.PinCheckRetryLeft;
}

/**
 * enable PIN lock
 * param (in) PinLockSet:  MMI_Modem_Pin_Lock_t*
 *       (in) PIN: INT8*, PIN code
 * return  void
 */
VOID  MMI_Modem_Enable_Pin_Lock(MMI_Modem_Pin_Lock_t * PinLockSet, INT8 * PIN)
{
    MMI_Modemadp_Enable_Pin_Lock_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Enable_Pin_Lock_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Enable_Pin_Lock_t));

    g_ModemSimMng.PinCheckSema = Hal_Sema_Create(0);
    pMsg->PinLockSet           = PinLockSet;
    pMsg->PIN                  = PIN;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_ENABLE_PIN_LOCK, (UI_Inter_Msg_t *)pMsg);
    g_ModemSimMng.PinCheckRetryLeft = 0;
    Hal_Sema_Lock(g_ModemSimMng.PinCheckSema);
    Hal_Sema_Destroy(g_ModemSimMng.PinCheckSema);

    // wait modem adaptor update the retried count and unlock semphore
    PinLockSet->RetriesLeft = g_ModemSimMng.PinCheckRetryLeft;
}

/**
 * enable PIN lock
 * param (in) PinLockQuery: MMI_Modem_Pin_Lock_t*
 * return  void
 */
VOID  MMI_Modem_Query_Pin_Lock(MMI_Modem_Pin_Lock_t * PinLockQuery)
{
    MMI_Modem_Pin_Lock_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modem_Pin_Lock_t *)Hal_Mem_Alloc(sizeof(MMI_Modem_Pin_Lock_t));
    memcpy(pMsg, PinLockQuery, sizeof(MMI_Modem_Pin_Lock_t));

    g_ModemSimMng.PinCheckSema = Hal_Sema_Create(0);
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_QUERY_PIN_LOCK, (UI_Inter_Msg_t *)pMsg);
    g_ModemSimMng.QueryPinLockType   = PinLockQuery->PinType;
    g_ModemSimMng.QueryPinLockResult = FALSE;
    g_ModemSimMng.PinCheckRetryLeft  = 0;
    Hal_Sema_Lock(g_ModemSimMng.PinCheckSema);

    // wait modem adaptor update the retried count and unlock semphore
    PinLockQuery->PinEnable   = g_ModemSimMng.QueryPinLockResult;
    PinLockQuery->RetriesLeft = g_ModemSimMng.PinCheckRetryLeft;
}

/**
 * Request modem adpter to try PIN code
 * param (in) simId:  MMI_MODEM_SIM_ID
 * param (in) PIN:  INT8*
 * return  UINT8: Retry Left: MMI_MODEM_PIN_CHECK_OK when check OK
 */
UINT8 MMI_Modem_Try_Pin_Req(MMI_MODEM_SIM_ID SimId, INT8 * PIN)
{
    MMI_Modemadp_Try_Pin_Req_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Try_Pin_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Try_Pin_Req_t));

    pMsg->SimId = SimId;
    pMsg->Code  = PIN;

    g_ModemSimMng.PinCheckSema = Hal_Sema_Create(0);
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_TRY_PIN_REQ, (UI_Inter_Msg_t *)pMsg);
    g_ModemSimMng.PinCheckRetryLeft = 0;
    Hal_Sema_Lock(g_ModemSimMng.PinCheckSema);
    Hal_Sema_Destroy(g_ModemSimMng.PinCheckSema);

    // wait modem adaptor update the tried count and unlock semphore
    return(g_ModemSimMng.PinCheckRetryLeft);
}

/**
 * Request modem adpter to try PIN2 code
 * param (in) simId:  MMI_MODEM_SIM_ID
 * param (in) PIN2:  INT8*
 * return UINT8: retry left: MMI_MODEM_PIN_CHECK_OK when check OK
 */
UINT8 MMI_Modem_Try_Pin2_Req(MMI_MODEM_SIM_ID SimId, INT8 * PIN2)
{
    MMI_Modemadp_Try_Pin_Req_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Try_Pin_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Try_Pin_Req_t));

    pMsg->SimId = SimId;
    pMsg->Code  = PIN2;

    g_ModemSimMng.PinCheckSema = Hal_Sema_Create(0);
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_TRY_PIN2_REQ, (UI_Inter_Msg_t *)pMsg);
    g_ModemSimMng.PinCheckRetryLeft = 0;
    Hal_Sema_Lock(g_ModemSimMng.PinCheckSema);

    // wait modem adaptor update the tried count and unlock semphore
    return(g_ModemSimMng.PinCheckRetryLeft);
}

/**
 * Request modem adpter to try PUK code
 * param (in) simId:  MMI_MODEM_SIM_ID
 *       (in) PUK:  INT8*
 *       (in) NewPIN: INT8*
 * return  UINT8: Retry Left: MMI_MODEM_PIN_CHECK_OK when check OK
 */
UINT8 MMI_Modem_Try_Puk_Req(MMI_MODEM_SIM_ID SimId, INT8 * PUK, INT8 * NewPIN)
{
    MMI_Modemadp_Try_Puk_Req_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Try_Puk_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Try_Puk_Req_t));

    pMsg->SimId  = SimId;
    pMsg->PUK    = PUK;
    pMsg->NewPIN = NewPIN;

    g_ModemSimMng.PinCheckSema = Hal_Sema_Create(0);
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_TRY_PUK_REQ, (UI_Inter_Msg_t *)pMsg);
    g_ModemSimMng.PinCheckRetryLeft = 0;
    Hal_Sema_Lock(g_ModemSimMng.PinCheckSema);

    // wait modem adaptor update the tried count and unlock semphore
    return(g_ModemSimMng.PinCheckRetryLeft);
}

/**
 * Request modem adpter to try PUK2 code
 * param (in) simId:  MMI_MODEM_SIM_ID
 *       (in) PUK2:  INT8*
 *       (in) NewPIN2: INT8*
 * return UINT8: retry left: MMI_MODEM_PIN_CHECK_OK when check OK
 */
UINT8 MMI_Modem_Try_Puk2_Req(MMI_MODEM_SIM_ID SimId, INT8 * PUK2, INT8 * NewPIN2)
{
    MMI_Modemadp_Try_Puk_Req_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Try_Puk_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Try_Puk_Req_t));

    pMsg->SimId  = SimId;
    pMsg->PUK    = PUK2;
    pMsg->NewPIN = NewPIN2;

    g_ModemSimMng.PinCheckSema = Hal_Sema_Create(0);
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_TRY_PUK2_REQ, (UI_Inter_Msg_t *)pMsg);
    g_ModemSimMng.PinCheckRetryLeft = 0;
    Hal_Sema_Lock(g_ModemSimMng.PinCheckSema);

    // wait modem adaptor update the tried count and unlock semphore
    return(g_ModemSimMng.PinCheckRetryLeft);
}

/**
 * Request modem adpter to change PIN code
 * param (in) simId:  MMI_MODEM_SIM_ID
 *       (in) PIN:    INT8*
 *       (in) OldPIN: INT8*
 * return  UINT8: Retry Left: MMI_MODEM_PIN_CHECK_OK when check OK
 */
UINT8 MMI_Modem_Change_Pin_Req(MMI_MODEM_SIM_ID SimId, INT8 * PIN, INT8 * OldPIN)
{
    MMI_Modemadp_Chg_Pin_Req_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Chg_Pin_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Chg_Pin_Req_t));

    pMsg->SimId  = SimId;
    pMsg->PIN    = PIN;
    pMsg->OldPIN = OldPIN;

    g_ModemSimMng.PinCheckSema = Hal_Sema_Create(0);
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_CHG_PIN_REQ, (UI_Inter_Msg_t *)pMsg);
    g_ModemSimMng.PinCheckRetryLeft = 0;
    Hal_Sema_Lock(g_ModemSimMng.PinCheckSema);
    Hal_Sema_Destroy(g_ModemSimMng.PinCheckSema);

    // wait modem adaptor update the tried count and unlock semphore
    return(g_ModemSimMng.PinCheckRetryLeft);
}

/**
 * Request modem adpter to change PIN2 code
 * param (in) simId:  MMI_MODEM_SIM_ID
 *       (in) PIN2:    INT8*
 *       (in) OldPIN2: INT8*
 * return  UINT8: Retry Left: MMI_MODEM_PIN_CHECK_OK when check OK
 */
UINT8 MMI_Modem_Change_Pin2_Req(MMI_MODEM_SIM_ID SimId, INT8 * PIN2, INT8 * OldPIN2)
{
    MMI_Modemadp_Chg_Pin_Req_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Chg_Pin_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Chg_Pin_Req_t));

    pMsg->SimId   = SimId;
    pMsg->PIN    = PIN2;
    pMsg->OldPIN = OldPIN2;

    g_ModemSimMng.PinCheckSema = Hal_Sema_Create(0);
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_CHG_PIN2_REQ, (UI_Inter_Msg_t *)pMsg);
    g_ModemSimMng.PinCheckRetryLeft = 0;
    Hal_Sema_Lock(g_ModemSimMng.PinCheckSema);

    // wait modem adaptor update the tried count and unlock semphore
    return(g_ModemSimMng.PinCheckRetryLeft);
}

/**
 * check whether sim is present.
 * param (in) SimId: MMI_MODEM_SIM_ID
 * return  BOOL: ture means sim present
 */
BOOL MMI_Modem_Sim_Present_Check_Req(MMI_MODEM_SIM_ID simId)
{
//    printf("##interface## %s\n", __FUNCTION__);

    return(g_ModemSimMng.SimPresent[simId]);
}

/**
 * Request CP phone book status
 * param (in) SimId: MMI_MODEM_SIM_ID
 * return  void
 */
VOID MMI_Modem_Get_Phonebook_Status_Req(MMI_MODEM_SIM_ID SimId)
{
    MMI_Modemadp_Get_Pb_Status_Req_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Get_Pb_Status_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Get_Pb_Status_Req_t));

    pMsg->SimId = SimId;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_GET_PB_STATUS_REQ, (UI_Inter_Msg_t *)pMsg);
}

/**
 * Request to write new contact into phone book
 * param (in) SimId: MMI_MODEM_SIM_ID
 * param (in) Contact: MMI_Modem_Contact*
 * return  void
 */
VOID MMI_Modem_Write_Phonebook_Req(MMI_MODEM_SIM_ID SimId, MMI_Modem_Contact * Contact)
{
    MMI_Modemadp_Write_Pb_Req_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Write_Pb_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Write_Pb_Req_t));

    pMsg->SimId   = SimId;
    pMsg->Contact = Contact;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_WRITE_PB_REQ, (UI_Inter_Msg_t *)pMsg);
}

/**
 * Request to read all of contacts from phone book
 * param (in) SimId: MMI_MODEM_SIM_ID
 * return  void
 */
VOID MMI_Modem_Read_Phonebook_Req(MMI_MODEM_SIM_ID SimId, UINT16 Used, UINT16 FirstIndex)
{
    MMI_Modemadp_Read_Pb_Req_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Read_Pb_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Read_Pb_Req_t));

    pMsg->SimId      = SimId;
    pMsg->Used       = Used;
    pMsg->FirstIndex = FirstIndex;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_READ_PB_REQ, (UI_Inter_Msg_t *)pMsg);
}

/**
 * Request to get IMSI
 * param (in) SimId: MMI_MODEM_SIM_ID
 * return  void
 */
VOID MMI_Modem_Get_Imsi_Req(MMI_MODEM_SIM_ID SimId)
{
    MMI_Modemadp_Get_Imsi_Req_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Get_Imsi_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Get_Imsi_Req_t));

    pMsg->SimId = SimId;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_GET_IMSI_REQ, (UI_Inter_Msg_t *)pMsg);
}

/**
 * Query Imsi
 * param (in) SimId: MMI_MODEM_SIM_ID
         (out) Operator: INT8 *
 * return  void
 */
VOID MMI_Modem_Query_Imsi_Req(MMI_MODEM_SIM_ID SimId, INT8 * Imsi)
{
    static INT8 tmp_imsi[8] = "Unknown";

    printf("##interface## %s\n", __FUNCTION__);

    if(NULL != g_ModemSimMng.Imsi[SimId]) {
        strncpy(Imsi, g_ModemSimMng.Imsi[SimId], 16);
    } else {
        Hal_Mem_Copy(Imsi, tmp_imsi, 8);
    }
}

/**
 * get ICCID request
 * param (in) void:
 * return  char *ICCID:if null means no sim or ICCID not present
*/
char * app_adaptor_get_iccid_info_req(void)
{
    char * Iccid = NULL;
    MMI_MODEM_SIM_ID sim_id = modem_sim_id;

    if(NULL != g_ModemSimMng.Iccid[sim_id]) {
        Iccid = (char *)Hal_Mem_Alloc(strlen(g_ModemSimMng.Iccid[sim_id]) + 1) ;
        Hal_Mem_Set(Iccid, 0, strlen(g_ModemSimMng.Iccid[sim_id]) + 1);
        Hal_Mem_Copy(Iccid, g_ModemSimMng.Iccid[sim_id], strlen(g_ModemSimMng.Iccid[sim_id]));
    }

    return Iccid;
}

static int convertRsrpToDbm(int n)
{
    if(n >= 0 && n <= 97)
        return -141 + n;
    else
        return INVALID_INT_MAX;
}

static int convertRxlevToDbm(int n)
{
    if(n >= 0 && n <= 64)
        return -113 + n;
    else
        return INVALID_INT_MAX;
}

/**
 * get Plmn request
 * param (in) void:
 * return  app_adp_reg_and_home_plmn_info_t* plmn:it contains register plmn and home plmn,
 * if plmn are 0,it means invaild.
*/
app_adp_reg_and_home_plmn_info_t * app_adaptor_get_plmn_info_req(void)
{
    app_adp_reg_and_home_plmn_info_t * RegHomePlmn = NULL;
    MMI_MODEM_SIM_ID sim_id = modem_sim_id;

    RegHomePlmn = (app_adp_reg_and_home_plmn_info_t *)Hal_Mem_Alloc(sizeof(app_adp_reg_and_home_plmn_info_t));
    Hal_Mem_Set(RegHomePlmn, 0, sizeof(app_adp_reg_and_home_plmn_info_t));
    Hal_Mem_Copy(RegHomePlmn, &g_ModemSimMng.RegHomePlmn[sim_id], sizeof(MODEMADP_SIM_REG_AND_HOME_PLMN_INFO));

    RegHomePlmn->rat = (watch_app_plmn_rat_type_t)g_ModemNwMng[sim_id].PlmnInfo.PlmnRat;
    printf("RegHomePlmn->rat is %d\n", RegHomePlmn->rat);

    if(APP_PLMN_RAT_GSM == RegHomePlmn->rat || APP_PLMN_RAT_UMTS == RegHomePlmn->rat) {
        RegHomePlmn->realdbm = convertRxlevToDbm(g_ModemSimMng.RegHomePlmn[sim_id].signalstrength * 2);
    } else if(APP_PLMN_RAT_LTE == RegHomePlmn->rat) {
        RegHomePlmn->realdbm = convertRsrpToDbm(g_ModemSimMng.RegHomePlmn[sim_id].signalstrength * 2);
    } else {
        RegHomePlmn->realdbm = INVALID_INT_MAX;
    }
    printf("RegHomePlmn->realdbm is %d\n", RegHomePlmn->realdbm);

    return RegHomePlmn;
}

/**
 * get phone number request
 * param (in) void:
 * return  char * phonenumber:if null means no sim or phone number not present
*/
char * app_adaptor_get_msisdn_info_req(void)
{
    char * PhoneNum = NULL;
    MMI_MODEM_SIM_ID sim_id = modem_sim_id;

    if(NULL != g_ModemSimMng.Msisdn[sim_id]) {
        PhoneNum = (char *)Hal_Mem_Alloc(strlen(g_ModemSimMng.Msisdn[sim_id]) + 1) ;
        Hal_Mem_Set(PhoneNum, 0, strlen(g_ModemSimMng.Msisdn[sim_id]) + 1);
        Hal_Mem_Copy(PhoneNum, g_ModemSimMng.Msisdn[sim_id], strlen(g_ModemSimMng.Msisdn[sim_id]));
    }

    return PhoneNum;
}

/**
 * Request to get ICCID
 * param (in) SimId: MMI_MODEM_SIM_ID
 * return  void
 */
VOID MMI_Modem_Get_Iccid_Req(MMI_MODEM_SIM_ID SimId)
{
    MMI_Modemadp_Get_Iccid_Req_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Get_Iccid_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Get_Iccid_Req_t));

    pMsg->SimId = SimId;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_GET_ICCID_REQ, (UI_Inter_Msg_t *)pMsg);
}

/**
 * Request to get PLMN
 * param (in) SimId: MMI_MODEM_SIM_ID
 * return  void
 */
VOID MMI_Modem_Get_Plmn_Req(MMI_MODEM_SIM_ID SimId)
{
    MMI_Modemadp_Get_Plmn_Req_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Get_Plmn_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Get_Plmn_Req_t));

    pMsg->SimId = SimId;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_GET_PLMN_REQ, (UI_Inter_Msg_t *)pMsg);
}

/**
 * Request to get MSISDN
 * param (in) SimId: MMI_MODEM_SIM_ID
 * return  void
 */
VOID MMI_Modem_Get_Msisdn_Req(MMI_MODEM_SIM_ID SimId)
{
    MMI_Modemadp_Get_Msisdn_Req_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Get_Msisdn_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Get_Msisdn_Req_t));

    pMsg->SimId = SimId;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_GET_MSISDN_REQ, (UI_Inter_Msg_t *)pMsg);
}

/**
 * Request to select TE character set(AT+CSCS="HEX")
 * param (in) SimId: MMI_MODEM_SIM_ID
 * return  void
 */
VOID MMI_Modem_Select_Character_Set_Req(MMI_MODEM_SIM_ID SimId)
{
    MMI_Modemadp_Sel_Char_Set_Req_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Sel_Char_Set_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Sel_Char_Set_Req_t));

    pMsg->SimId = SimId;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_SEL_CHAR_SET_REQ, (UI_Inter_Msg_t *)pMsg);
}

/**
 * Request to do sim soft reset
 * param (in) SimId: MMI_MODEM_SIM_ID
 * return  void
 */
VOID MMI_Modem_Sim_Soft_Reset_Req(MMI_MODEM_SIM_ID SimId)
{
    MMI_Modemadp_Sim_Soft_Reset_Req_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (MMI_Modemadp_Sim_Soft_Reset_Req_t *)Hal_Mem_Alloc(sizeof(MMI_Modemadp_Sim_Soft_Reset_Req_t));

    pMsg->SimId = SimId;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, MMI_MODEMADP_SIM_SOFT_RESET_REQ, (UI_Inter_Msg_t *)pMsg);
}

/******************************************************************************
 *
 *         RIL -> Modem Adaptor
 *
 *******************************************************************************/

/**
 * RIL_UNSOL_CP_PHONEBOOK_INITED
 * Indicate When phonebook has been loaded by CP
 * param   RIL_SIM_ID SimId: SIM ID
 * return  void
 */
VOID Ril_SIM_Response_Cp_Phonebook_Inited(RIL_SIM_ID SimId)
{
    RIL_Modemadp_Rsp_Cp_Pb_Inited_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Cp_Pb_Inited_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Cp_Pb_Inited_t));

    pMsg->SimId = SimId;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_CP_PB_INITED, (UI_Inter_Msg_t *)pMsg);
}

/**
 * RIL_RESPONSE_GET_CP_PHONEBOOK_STATUS(AT+CPBS?)
 * Response for getting CP phonebook status
 * param   RIL_SIM_ID SimId: SIM ID
 *         CPPhoneBook *p_phonebook: phonebook status
 *         RIL_Errno result: RIL_E_SUCCESS, RIL_E_MODEM_ERR
 * return  void
 */
VOID Ril_SIM_Response_Get_Phonebook_Status(RIL_SIM_ID SimId, CPPhoneBook * p_phonebook, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Get_Pb_Status_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Get_Pb_Status_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Get_Pb_Status_t));

    pMsg->SimId       = SimId;
    pMsg->p_phonebook = p_phonebook;
    pMsg->result      = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_GET_PB_STATUS, (UI_Inter_Msg_t *)pMsg);
}

/**
 * RIL_RESPONSE_WRITE_PHONEBOOK(AT+CPBW=)
 * Response for writting new contact into phonebook
 * param   RIL_SIM_ID SimId: SIM ID
 *         INT32 *p_writtenIndex: the written index of the contact
 *         RIL_Errno result: RIL_E_SUCCESS, RIL_E_MODEM_ERR
 * return  void
 */
VOID Ril_SIM_Response_Write_Phonebook(RIL_SIM_ID SimId, INT32 * p_writtenIndex, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Write_Pb_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Write_Pb_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Write_Pb_t));

    pMsg->SimId          = SimId;
    pMsg->p_writtenIndex = p_writtenIndex;
    pMsg->result         = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_WRITE_PB, (UI_Inter_Msg_t *)pMsg);
}

/**
 * RIL_RESPONSE_READ_PHONEBOOK(AT+CPBR=)
 * Response for reading contacts from phonebook
 * param   RIL_SIM_ID SimId: SIM ID
 *         RIL_Contact *p_contact: the contact read from phonebook
 *         int count: the count of contacts
 *         RIL_Errno result: RIL_E_SUCCESS, RIL_E_MODEM_ERR
 * return  void
 */
VOID Ril_SIM_Response_Read_Phonebook(RIL_SIM_ID SimId, RIL_Contact * p_contact, int count, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Read_Pb_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Read_Pb_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Read_Pb_t));

    pMsg->SimId     = SimId;
    pMsg->p_contact = p_contact;
    pMsg->count     = count;
    pMsg->result    = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_READ_PB, (UI_Inter_Msg_t *)pMsg);
}

/**
 * RIL_RESPONSE_GET_SIM_STATUS(AT+CPIN?)
 * Response for getting status of the SIM interface and the SIM card
 * param   RIL_SIM_ID SimId: SIM ID
 *         RIL_CardStatus *p_card_status: SIM card status
 *         RIL_Errno result: RIL_E_SUCCESS, RIL_E_MODEM_ERR
 * return  void
 */
VOID Ril_SIM_Response_Get_Sim_Status(RIL_SIM_ID SimId, RIL_CardStatus * p_cardStatus, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Get_Sim_Status_t * pMsg;

    printf("##interface## %s, simid %d, status %d\n", __FUNCTION__, SimId, p_cardStatus->card_state);

    pMsg = (RIL_Modemadp_Rsp_Get_Sim_Status_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Get_Sim_Status_t));

    pMsg->SimId        = SimId;
    pMsg->p_cardStatus = p_cardStatus;
    pMsg->result       = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_GET_SIM_STATUS, (UI_Inter_Msg_t *)pMsg);
}

/**
 * RIL_REQUEST_LOCK_INFO(AT+EPIN)
 * Response for getting PIN/PUK lock info
 * param   RIL_SIM_ID SimId: SIM ID
 *         int *response:
 * ((int *)response)[0] PIN1 retried left
 * ((int *)response)[1] PIN2 retried left
 * ((int *)response)[2] PUK1 retried left
 * ((int *)response)[3] PUK2 retried left
 *         RIL_Errno result: RIL_E_SUCCESS, RIL_E_MODEM_ERR
 * return  void
 */
VOID Ril_SIM_Response_Query_Lock_Info(RIL_SIM_ID SimId, INT32 * response, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Query_Lock_Info_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Query_Lock_Info_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Query_Lock_Info_t));

    pMsg->SimId    = SimId;
    pMsg->response = response;
    pMsg->result   = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_QUERY_LOCK_INFO, (UI_Inter_Msg_t *)pMsg);
}

/**
 * RIL_RESPONSE_ENTER_SIM_PIN(AT+CPIN=)
 * Response for entering SIM PIN
 * param   RIL_SIM_ID SimId: SIM ID
 *         INT32 *p_retryLeft: the number of retries remaining, or -1 if unknown
 * return  RIL_Errno(RIL_E_SUCCESS, RIL_E_INTERNAL_ERR and CME errors)
 */
VOID Ril_SIM_Response_Enter_Sim_Pin(RIL_SIM_ID SimId, INT32 * p_retryLeft, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Enter_Sim_Pin_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Enter_Sim_Pin_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Enter_Sim_Pin_t));

    pMsg->SimId       = SimId;
    pMsg->p_retryLeft = p_retryLeft;
    pMsg->result      = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_ENTER_SIM_PIN, (UI_Inter_Msg_t *)pMsg);
}

/**
* RIL_REQUEST_QUERY_FACILITY_LOCK (AT+CLCK=)
* response query facility lock result
* param
* return RIL_Errno
*/
void Ril_SIM_Response_Query_Facility_Lock(RIL_SIM_ID SimId, int *data, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Query_Facility_Lock_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Query_Facility_Lock_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Query_Facility_Lock_t));

    pMsg->SimId    = SimId;
    pMsg->data = data;
    pMsg->result   = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_RSP_QUERY_FACILITY_LOCK, (UI_Inter_Msg_t *)pMsg);
}

/**
* RIL_REQUEST_SET_FACILITY_LOCK (AT+CLCK=)
* response query facility lock result
* param
* return RIL_Errno
*/
void Ril_SIM_Response_Set_Facility_Lock(RIL_SIM_ID SimId, int *data, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Set_Facility_Lock_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Set_Facility_Lock_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Set_Facility_Lock_t));

    pMsg->SimId    = SimId;
    pMsg->data = data;
    pMsg->result   = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_RSP_SET_FACILITY_LOCK, (UI_Inter_Msg_t *)pMsg);
}

/**
 * RIL_RESPONSE_ENTER_SIM_PUK(ATD**05*)
 * Response for entering SIM PUK and new PIN
 * param   RIL_SIM_ID SimId: SIM ID
 *         INT32 *p_retryLeft: the number of retries remaining, or -1 if unknown
 *         RIL_Errno result: RIL_E_SUCCESS, RIL_E_INTERNAL_ERR, RIL_E_MODEM_ERR and CME errors
 * return  void
 */
VOID Ril_SIM_Response_Enter_Sim_Puk(RIL_SIM_ID SimId, INT32 * p_retryLeft, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Enter_Sim_Pin_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Enter_Sim_Pin_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Enter_Sim_Pin_t));

    pMsg->SimId       = SimId;
    pMsg->p_retryLeft = p_retryLeft;
    pMsg->result      = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_ENTER_SIM_PUK, (UI_Inter_Msg_t *)pMsg);
}

/**
 * RIL_RESPONSE_ENTER_SIM_PIN2(AT+CPIN=)
 * Response for entering SIM PIN2
 * param   RIL_SIM_ID SimId: SIM ID
 *         INT32 *p_retryLeft: the number of retries remaining, or -1 if unknown
 *         RIL_Errno result: RIL_E_SUCCESS, RIL_E_INTERNAL_ERR and CME errors
 * return  void
 */
VOID Ril_SIM_Response_Enter_Sim_Pin2(RIL_SIM_ID SimId, INT32 * p_retryLeft, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Enter_Sim_Pin_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Enter_Sim_Pin_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Enter_Sim_Pin_t));

    pMsg->SimId       = SimId;
    pMsg->p_retryLeft = p_retryLeft;
    pMsg->result      = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_ENTER_SIM_PIN2, (UI_Inter_Msg_t *)pMsg);
}

/**
 * RIL_RESPONSE_ENTER_SIM_PUK2(ATD**052*)
 * Response for entering SIM PUK2 and new PIN2.
 * param   RIL_SIM_ID SimId: SIM ID
 *         INT32 *p_retryLeft: the number of retries remaining, or -1 if unknown
 *         RIL_Errno result: RIL_E_SUCCESS, RIL_E_INTERNAL_ERR, RIL_E_MODEM_ERR and other errors
 * return  void
 */
VOID Ril_SIM_Response_Enter_Sim_Puk2(RIL_SIM_ID SimId, INT32 * p_retryLeft, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Enter_Sim_Pin_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Enter_Sim_Pin_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Enter_Sim_Pin_t));

    pMsg->SimId       = SimId;
    pMsg->p_retryLeft = p_retryLeft;
    pMsg->result      = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_ENTER_SIM_PUK2, (UI_Inter_Msg_t *)pMsg);
}

/**
 * RIL_RESPONSE_CHANGE_SIM_PIN(AT+CPIN= , )
 * Response for entering old SIM PIN and new PIN.
 * param   RIL_SIM_ID SimId: SIM ID
 *         INT32 *p_retryLeft: the number of retries remaining, or -1 if unknown
 *         RIL_Errno result: RIL_E_SUCCESS, RIL_E_INTERNAL_ERR and CME errors
 * return  void
 */
VOID Ril_SIM_Response_Change_Sim_Pin(RIL_SIM_ID SimId, INT32 * p_retryLeft, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Enter_Sim_Pin_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Enter_Sim_Pin_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Enter_Sim_Pin_t));

    pMsg->SimId       = SimId;
    pMsg->p_retryLeft = p_retryLeft;
    pMsg->result      = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_CHG_SIM_PIN, (UI_Inter_Msg_t *)pMsg);
}


/**
 * RIL_RESPONSE_CHANGE_SIM_PIN2(AT+CPWD=\"P2\", , )
 * Response for entering old SIM PIN2 and new PIN2.
 * param   RIL_SIM_ID SimId: SIM ID
 *         INT32 *p_retryLeft: the number of retries remaining, or -1 if unknown
 *         RIL_Errno result: RIL_E_SUCCESS, RIL_E_INTERNAL_ERR and CME errors
 * return  void
 */
VOID Ril_SIM_Response_Change_Sim_Pin2(RIL_SIM_ID SimId, INT32 * p_retryLeft, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Enter_Sim_Pin_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Enter_Sim_Pin_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Enter_Sim_Pin_t));

    pMsg->SimId       = SimId;
    pMsg->p_retryLeft = p_retryLeft;
    pMsg->result      = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_CHG_SIM_PIN2, (UI_Inter_Msg_t *)pMsg);
}

/**
 * RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED(AT*EUICC?)
 * Indicates that SIM state changes.
 * Callee will invoke RIL_REQUEST_GET_SIM_STATUS on main thread
 * param   RIL_SIM_ID SimId: SIM ID
 * return  void
 */
VOID Ril_SIM_Response_Sim_Status_Changed(RIL_SIM_ID SimId)
{
    RIL_Modemadp_Rsp_Sim_Status_Changed_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Sim_Status_Changed_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Sim_Status_Changed_t));

    pMsg->SimId = SimId;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_STATUS_CHANGED, (UI_Inter_Msg_t *)pMsg);
}

/**
 * RIL_RESPONSE_GET_IMSI(AT+CIMI)
 * Response for getting the SIM IMSI
 * param   RIL_SIM_ID SimId: SIM ID
 *         char *p_imsi: IMSI
 *         RIL_Errno result: RIL_E_SUCCESS, RIL_E_MODEM_ERR
 * return  void
 */
VOID Ril_SIM_Response_Get_Imsi(RIL_SIM_ID SimId, char * p_imsi, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Get_Imsi_t * pMsg;

    printf("##interface## %s, p_imsi=%s\n", __FUNCTION__, p_imsi);

    pMsg = (RIL_Modemadp_Rsp_Get_Imsi_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Get_Imsi_t));

    pMsg->SimId  = SimId;
    pMsg->p_imsi = p_imsi;
    pMsg->result = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_GET_IMSI, (UI_Inter_Msg_t *)pMsg);
}

/**
 * Ril_SIM_Response_Get_Iccid(AT*ICCID?)
 * Query sim ICCID.
 * param   RIL_SIM_ID SimId: SIM ID
 *         p_iccid: output
 * return  void
 */
void Ril_SIM_Response_Get_Iccid(RIL_SIM_ID SimId, char * p_iccid, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Get_Iccid_t * pMsg;

    printf("##interface## %s, p_iccid=%s\n", __FUNCTION__, p_iccid);

    pMsg = (RIL_Modemadp_Rsp_Get_Iccid_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Get_Iccid_t));

    pMsg->SimId  = SimId;
    pMsg->p_iccid = p_iccid;
    pMsg->result = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_GET_ICCID, (UI_Inter_Msg_t *)pMsg);
}
/**
 * Ril_SIM_Response_Get_Plmn(AT+ZDON?)
 * Query sim ICCID.
 * param   RIL_SIM_ID SimId: SIM ID
 *         p_plmn: output
 * return  void
 */
void Ril_SIM_Response_Get_Plmn(RIL_SIM_ID SimId, RIL_SIM_Plmn_Info * p_plmn, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Get_Plmn_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Get_Plmn_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Get_Plmn_t));

    pMsg->SimId  = SimId;
    pMsg->p_plmn = p_plmn;
    pMsg->result = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_GET_PLMN, (UI_Inter_Msg_t *)pMsg);
}
/**
 * Ril_SIM_Response_Get_Msisdn(AT*CNUM)
 * Query sim ICCID.
 * param   RIL_SIM_ID SimId: SIM ID
 *         p_msisdn: output
 * return  void
 */
void Ril_SIM_Response_Get_Msisdn(RIL_SIM_ID SimId, char * p_msisdn, RIL_Errno result)
{
    RIL_Modemadp_Rsp_Get_Msisdn_t * pMsg;

    printf("##interface## %s\n", __FUNCTION__);

    pMsg = (RIL_Modemadp_Rsp_Get_Msisdn_t *)Hal_Mem_Alloc(sizeof(RIL_Modemadp_Rsp_Get_Msisdn_t));

    pMsg->SimId  = SimId;
    pMsg->p_msisdn = p_msisdn;
    pMsg->result = result;
    UI_Send_Inter_Msg(UI_SUB_MODULE_MODEM_ADP, RIL_MODEMADP_SIM_RSP_GET_MSISDN, (UI_Inter_Msg_t *)pMsg);
}

/******************************************************************************
 *
 *         Message Handler of MMI -> Modem Adaptor
 *
 *******************************************************************************/

/**
 * Handler of MMI_MODEMADP_QUERY_FACILITY_PIN_LOCK
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Query_Facility_Pin_Lock_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Query_Facility_Pin_Lock_t * QueryFacPinLock;
    UINT8                              *data[3];
    INT32                             response;

    QueryFacPinLock = (MMI_Modemadp_Query_Facility_Pin_Lock_t *)Msg;
    Hal_Sema_Lock(g_ModemSimMng.PinCheckSema);

    data[0] = Hal_Mem_Alloc(sizeof(UINT8) * 3);
    Hal_Mem_Set(data[0],0,sizeof(UINT8) * 3);
    Hal_Mem_Copy(data[0],"SC",2);

    data[1] = Hal_Mem_Alloc(sizeof(UINT8));
    Hal_Mem_Set(data[1],0,sizeof(UINT8));

    // 11 = 1(voice) + 2(data) + 8(short message)
    data[2] = Hal_Mem_Alloc(sizeof(UINT8) * 3);
    Hal_Mem_Set(data[2],0,sizeof(UINT8) * 3);
    data[2][0] = '1';
    data[2][1] = '1';

    Ril_SIM_Request_Query_Facility_Lock(MMI_ModemAdp_Get_Ril_SimId(QueryFacPinLock->PinLockSet->SimId), (const UINT8 **)data, &response);
    Hal_Mem_Free(data[0]);
    Hal_Mem_Free(data[1]);
    Hal_Mem_Free(data[2]);
}

/**
 * Handler of MMI_MODEMADP_ENABLE_PIN_LOCK
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Enable_Pin_Lock_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Enable_Pin_Lock_t * EnablePinLock;
    INT8                              *data[4];
    INT32                             response;

    EnablePinLock = (MMI_Modemadp_Enable_Pin_Lock_t *)Msg;
    Hal_Sema_Lock(g_ModemSimMng.PinCheckSema);

    data[0] = Hal_Mem_Alloc(sizeof(INT8) * 3);
    Hal_Mem_Set(data[0],0,sizeof(INT8) * 3);
    Hal_Mem_Copy(data[0],"SC",2);

    data[1] = Hal_Mem_Alloc(sizeof(INT8) * 2);
    Hal_Mem_Set(data[1],0,sizeof(INT8) * 2);
    if(EnablePinLock->PinLockSet->PinEnable) {
        data[1][0] = '1';
    } else {
        data[1][0] = '0';
    }

    data[2] = Hal_Mem_Alloc(sizeof(INT8) * 9);
    Hal_Mem_Set(data[2],0,sizeof(INT8) * 9);
    memcpy(data[2], EnablePinLock->PIN, strlen(EnablePinLock->PIN));

    // 11 = 1(voice) + 2(data) + 8(short message)
    data[3] = Hal_Mem_Alloc(sizeof(INT8) * 3);
    Hal_Mem_Set(data[3],0,sizeof(INT8) * 3);
    data[3][0] = '1';
    data[3][1] = '1';

    Ril_SIM_Request_Set_Facility_Lock(MMI_ModemAdp_Get_Ril_SimId(EnablePinLock->PinLockSet->SimId), (const INT8 **)data, &response);
    Hal_Mem_Free(data[0]);
    Hal_Mem_Free(data[1]);
    Hal_Mem_Free(data[2]);
    Hal_Mem_Free(data[3]);

    Hal_Mem_Free(EnablePinLock->PIN);
}

/**
 * Handler of MMI_MODEMADP_QUERY_PIN_LOCK
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Query_Pin_Lock_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modem_Pin_Lock_t * QueryPinLock;

    QueryPinLock = (MMI_Modem_Pin_Lock_t *)Msg;

    Ril_SIM_Request_Get_Sim_Status(MMI_ModemAdp_Get_Ril_SimId(QueryPinLock->SimId));
    g_ModemSimMng.SimOperationStatus[QueryPinLock->SimId] = MODEMADP_SIM_PIN_LOCK_QUERYING;
}

/**
 * Handler of MMI_MODEMADP_TRY_PIN_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Try_Pin_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Try_Pin_Req_t * TryPinReq;

    TryPinReq = (MMI_Modemadp_Try_Pin_Req_t *)Msg;

    Hal_Sema_Lock(g_ModemSimMng.PinCheckSema);

    Ril_SIM_Request_Enter_Sim_Pin(MMI_ModemAdp_Get_Ril_SimId(TryPinReq->SimId), TryPinReq->Code);
    if (TryPinReq->Code) {
        Hal_Mem_Free(TryPinReq->Code);
    }
    g_ModemSimMng.SimOperationStatus[TryPinReq->SimId] = MODEMADP_SIM_PIN_TRYING;
}

/**
 * Handler of MMI_MODEMADP_TRY_PIN2_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Try_Pin2_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Try_Pin_Req_t * TryPin2Req;

    TryPin2Req = (MMI_Modemadp_Try_Pin_Req_t *)Msg;

    Ril_SIM_Request_Enter_Sim_Pin2(MMI_ModemAdp_Get_Ril_SimId(TryPin2Req->SimId), TryPin2Req->Code);
    g_ModemSimMng.SimOperationStatus[TryPin2Req->SimId] = MODEMADP_SIM_PIN2_TRYING;
}

/**
 * Handler of MMI_MODEMADP_TRY_PUK_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Try_Puk_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Try_Puk_Req_t * TryPukReq;
    RIL_SIM_REQUEST_ENTER_PUK   *  p_pukAndPin;

    TryPukReq = (MMI_Modemadp_Try_Puk_Req_t *)Msg;

    p_pukAndPin           = (RIL_SIM_REQUEST_ENTER_PUK *)Hal_Mem_Alloc(sizeof(RIL_SIM_REQUEST_ENTER_PUK));
    p_pukAndPin->p_puk    = TryPukReq->PUK;
    p_pukAndPin->p_newPin = TryPukReq->NewPIN;
    Ril_SIM_Request_Enter_Sim_Puk(MMI_ModemAdp_Get_Ril_SimId(TryPukReq->SimId), p_pukAndPin);
    g_ModemSimMng.SimOperationStatus[TryPukReq->SimId] = MODEMADP_SIM_PUK_TRYING;
}

/**
 * Handler of MMI_MODEMADP_TRY_PUK2_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Try_Puk2_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Try_Puk_Req_t * TryPuk2Req;
    RIL_SIM_REQUEST_ENTER_PUK   *  p_pukAndPin;

    TryPuk2Req = (MMI_Modemadp_Try_Puk_Req_t *)Msg;

    p_pukAndPin           = (RIL_SIM_REQUEST_ENTER_PUK *)Hal_Mem_Alloc(sizeof(RIL_SIM_REQUEST_ENTER_PUK));
    p_pukAndPin->p_puk    = TryPuk2Req->PUK;
    p_pukAndPin->p_newPin = TryPuk2Req->NewPIN;
    Ril_SIM_Request_Enter_Sim_Puk2(MMI_ModemAdp_Get_Ril_SimId(TryPuk2Req->SimId), p_pukAndPin);
    g_ModemSimMng.SimOperationStatus[TryPuk2Req->SimId] = MODEMADP_SIM_PUK2_TRYING;
}

/**
 * Handler of MMI_MODEMADP_CHG_PIN_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Change_Pin_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Chg_Pin_Req_t * ChgPinReq;
    RIL_SIM_REQUEST_CHANGE_PIN  *  p_changePin;

    ChgPinReq = (MMI_Modemadp_Chg_Pin_Req_t *)Msg;

    Hal_Sema_Lock(g_ModemSimMng.PinCheckSema);

    p_changePin           = (RIL_SIM_REQUEST_CHANGE_PIN *)Hal_Mem_Alloc(sizeof(RIL_SIM_REQUEST_CHANGE_PIN));
    p_changePin->p_oldPin = ChgPinReq->OldPIN;
    p_changePin->p_newPin = ChgPinReq->PIN;

    Ril_SIM_Request_Change_Sim_Pin(MMI_ModemAdp_Get_Ril_SimId(ChgPinReq->SimId), p_changePin);
    if (p_changePin->p_oldPin) {
        Hal_Mem_Free(p_changePin->p_oldPin);
    }
    if (p_changePin->p_newPin) {
        Hal_Mem_Free(p_changePin->p_newPin);
    }
    Hal_Mem_Free(p_changePin);
    g_ModemSimMng.SimOperationStatus[ChgPinReq->SimId] = MODEMADP_SIM_PIN_CHANGING;
}

/**
 * Handler of MMI_MODEMADP_CHG_PIN2_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Change_Pin2_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Chg_Pin_Req_t * ChgPin2Req;
    RIL_SIM_REQUEST_CHANGE_PIN  *  p_changePin;

    ChgPin2Req = (MMI_Modemadp_Chg_Pin_Req_t *)Msg;

    p_changePin           = (RIL_SIM_REQUEST_CHANGE_PIN *)Hal_Mem_Alloc(sizeof(RIL_SIM_REQUEST_CHANGE_PIN));
    p_changePin->p_oldPin = ChgPin2Req->OldPIN;
    p_changePin->p_newPin = ChgPin2Req->PIN;

    Ril_SIM_Request_Change_Sim_Pin2(MMI_ModemAdp_Get_Ril_SimId(ChgPin2Req->SimId), p_changePin);
    g_ModemSimMng.SimOperationStatus[ChgPin2Req->SimId] = MODEMADP_SIM_PIN2_CHANGING;
}

/**
 * Handler of MMI_MODEMADP_SIM_PRESENT_CHK_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Sim_Present_Check_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Sim_Present_Chk_Req_t * SimPresentChkReq;

    SimPresentChkReq = (MMI_Modemadp_Sim_Present_Chk_Req_t *)Msg;

    Ril_SIM_Request_Get_Sim_Status(MMI_ModemAdp_Get_Ril_SimId(SimPresentChkReq->SimId));
    g_ModemSimMng.SimOperationStatus[SimPresentChkReq->SimId] = MODEMADP_SIM_SIM_PRESENT_CHECKING;
}

/**
 * Handler of MMI_MODEMADP_GET_PB_STATUS_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Get_Pb_Status_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Get_Pb_Status_Req_t * GetPbStatusReq;

    GetPbStatusReq = (MMI_Modemadp_Get_Pb_Status_Req_t *)Msg;

    Ril_SIM_Request_Get_Phonebook_Status(MMI_ModemAdp_Get_Ril_SimId(GetPbStatusReq->SimId));
}

/**
 * Handler of MMI_MODEMADP_WRITE_PB_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Write_Pb_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Write_Pb_Req_t * WritePbReq;
    RIL_Contact                 * p_contact;

    WritePbReq = (MMI_Modemadp_Write_Pb_Req_t *)Msg;

    // set the contact to be written
    p_contact = (RIL_Contact *)Hal_Mem_Alloc(sizeof(RIL_Contact));
    if(MMI_MODEM_CONTACT_INDEX_INVALID != WritePbReq->Contact->Index) {
        // delete the contact from phonebook
        Hal_Mem_Set(p_contact, 0, sizeof(RIL_Contact));
        p_contact->index = WritePbReq->Contact->Index;
    }
    // store a new contact, and need find an empty entry first
    // need to set at+cscs="HEX"
    else {
        p_contact->tel_number = Hal_Mem_Alloc(MMI_MODEM_MAX_NUMBER_LEN);
        memcpy(p_contact->tel_number, WritePbReq->Contact->Number, MMI_MODEM_MAX_NUMBER_LEN);
        UINT8 NameLen = strlen(WritePbReq->Contact->Name);
        UINT8 i = 0;
        while ('\0' != WritePbReq->Contact->Name[i])
        {
            /*Normal ASCII*/
            if(0 == (WritePbReq->Contact->Name[i] & 0x80))
            {
                i++;
            }
            /*Real UTF-8 decode*/
            else if ((0xC0 == (WritePbReq->Contact->Name[i] & 0xE0))     /*2 bytes UTF-8 code*/
                     || (0xE0 == (WritePbReq->Contact->Name[i] & 0xF0))  /*3 bytes UTF-8 code*/
                     || (0xF0 == (WritePbReq->Contact->Name[i] & 0xF8))) /*4 bytes UTF-8 code*/
            {
                break;
            }
            else
            {
                i++; /*Not UTF-8 char. Go the next.*/
            }
        }
        if ('\0' != WritePbReq->Contact->Name[i]) /*UTF-8 code*/
        {
            p_contact->name = Hal_Mem_Alloc(MMI_MODEM_MAX_NAME_LEN * 4 + 2);
            memset(p_contact->name, 0, MMI_MODEM_MAX_NAME_LEN * 4 + 2);
            i = 0;
            p_contact->name[i++] = '8';
            p_contact->name[i++] = '0';
            UINT16 Ucs2Str[MMI_MODEM_MAX_NAME_LEN] = {0};
            UINT8 Ucs2Len = lv_txt_utf8_2_unicode((const char *)WritePbReq->Contact->Name,
                                                  strlen(WritePbReq->Contact->Name),
                                                  Ucs2Str) / sizeof(UINT16);
            for (UINT8 j = 0;j < Ucs2Len;j++)
            {
                snprintf(&p_contact->name[i], 5, "%04X", Ucs2Str[j]);
                i += 4;
            }
            p_contact->name[i] = '\0';
        }
        else
        {
            p_contact->name = Hal_Mem_Alloc(MMI_MODEM_MAX_NAME_LEN * 2);
            memset(p_contact->name, 0, MMI_MODEM_MAX_NAME_LEN * 2);
            i = 0;
            while ('\0' != WritePbReq->Contact->Name[i])
            {
                snprintf(&p_contact->name[i * 2], 3, "%02X", WritePbReq->Contact->Name[i]);
                i++;
            }
        }
        printf("%s: name %s\n", __FUNCTION__, p_contact->name);
    }
    // write the contact into pb
    Ril_SIM_Request_Write_Phonebook(MMI_ModemAdp_Get_Ril_SimId(WritePbReq->SimId), p_contact);
    if (p_contact->tel_number) {
        Hal_Mem_Free(p_contact->tel_number);
    }
    if (p_contact->name) {
        Hal_Mem_Free(p_contact->name);
    }
    Hal_Mem_Free(p_contact);
    Hal_Mem_Free(WritePbReq->Contact);
}

/**
 * Handler of MMI_MODEMADP_READ_PB_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Read_Pb_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Read_Pb_Req_t * ReadPbReq;
    RIL_ReadPbIndex              Index;
    UINT16                       ReadPbNum;

    ReadPbReq = (MMI_Modemadp_Read_Pb_Req_t *)Msg;

    // pb is not empty
    if(0 != ReadPbReq->Used) {
        g_ModemSimMng.ContactsUsed[ReadPbReq->SimId] = ReadPbReq->Used;

        if (MMI_MODEMADP_SIM_READ_PB_NUM < ReadPbReq->Used) {
            ReadPbNum = MMI_MODEMADP_SIM_READ_PB_NUM;
        }
        else
        {
            ReadPbNum = ReadPbReq->Used;
        }
        // prepare the buffer to store all contacts
        g_ModemSimMng.ReadPbContact[ReadPbReq->SimId] =
            (MMI_Modem_Contact *)Hal_Mem_Alloc(sizeof(MMI_Modem_Contact) * ReadPbNum);

        // read the contacts
        Index.firstIndex = ReadPbReq->FirstIndex;
        Index.endIndex   = ReadPbReq->FirstIndex + ReadPbNum - 1;
        Ril_SIM_Request_Read_Phonebook(MMI_ModemAdp_Get_Ril_SimId(ReadPbReq->SimId), Index);
    }
    // pb is empty
    else {
        MMI_Modem_Read_Phonebook_Cnf(ReadPbReq->SimId, NULL, 0, TRUE);
    }
}

/**
 * Handler of MMI_MODEMADP_GET_IMSI_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Get_Imsi_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Get_Imsi_Req_t * GetImsiReq;

    GetImsiReq = (MMI_Modemadp_Get_Imsi_Req_t *)Msg;

    Ril_SIM_Request_Get_Imsi(MMI_ModemAdp_Get_Ril_SimId(GetImsiReq->SimId));
    g_ModemSimMng.SimOperationStatus[GetImsiReq->SimId] = MODEMADP_SIM_IMSI_GETTING;
}

/**
 * Handler of MMI_MODEMADP_GET_ICCID_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Get_Iccid_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Get_Iccid_Req_t * GetIccidReq;

    GetIccidReq = (MMI_Modemadp_Get_Iccid_Req_t *)Msg;

    Ril_SIM_Request_Query_Iccid(MMI_ModemAdp_Get_Ril_SimId(GetIccidReq->SimId));
    g_ModemSimMng.SimOperationStatus[GetIccidReq->SimId] = MODEMADP_SIM_GET_ICCID;
}

/**
 * Handler of MMI_MODEMADP_GET_PLMN_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Get_Plmn_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Get_Plmn_Req_t * GetPlmnReq;

    GetPlmnReq = (MMI_Modemadp_Get_Plmn_Req_t *)Msg;

    Ril_SIM_Request_Query_Plmn(MMI_ModemAdp_Get_Ril_SimId(GetPlmnReq->SimId));
    g_ModemSimMng.SimOperationStatus[GetPlmnReq->SimId] = MODEMADP_SIM_GET_PLMN;
}

/**
 * Handler of MMI_MODEMADP_GET_MSISDN_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Get_Msisdn_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Get_Msisdn_Req_t * GetMsisdnReq;

    GetMsisdnReq = (MMI_Modemadp_Get_Msisdn_Req_t *)Msg;

    Ril_SIM_Request_Query_Msisdn(MMI_ModemAdp_Get_Ril_SimId(GetMsisdnReq->SimId));
    g_ModemSimMng.SimOperationStatus[GetMsisdnReq->SimId] = MODEMADP_SIM_GET_MSISDN;
}

/**
 * Handler of MMI_MODEMADP_SEL_CHAR_SET_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Sel_Char_Set_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Sel_Char_Set_Req_t * SelCharSetReq;

    SelCharSetReq = (MMI_Modemadp_Sel_Char_Set_Req_t *)Msg;

    Ril_SIM_Request_Set_Cscs(MMI_ModemAdp_Get_Ril_SimId(SelCharSetReq->SimId));
}

/**
 * Handler of MMI_MODEMADP_SEL_CHAR_SET_REQ
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Sim_Soft_Reset_Req_Handler(UI_Inter_Msg_t * Msg)
{
    MMI_Modemadp_Sim_Soft_Reset_Req_t * SimSoftReset;

    SimSoftReset = (MMI_Modemadp_Sim_Soft_Reset_Req_t *)Msg;

    Hal_Mem_Free(g_ModemSimMng.Imsi[SimSoftReset->SimId]);
    g_ModemSimMng.Imsi[SimSoftReset->SimId] = NULL;
    Hal_Mem_Free(g_ModemSimMng.Iccid[SimSoftReset->SimId]);
    g_ModemSimMng.Iccid[SimSoftReset->SimId] = NULL;

    Ril_SIM_Request_Soft_Reset(MMI_ModemAdp_Get_Ril_SimId(SimSoftReset->SimId));
}

/******************************************************************************
 *
 *         Message Handler of RIL -> Modem Adaptor
 *
 *******************************************************************************/

/**
 * Handler of RIL_MODEMADP_SIM_RSP_CP_PB_INITED
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Cp_Pb_Inited_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Cp_Pb_Inited_t * CpPbInited;

    CpPbInited = (RIL_Modemadp_Rsp_Cp_Pb_Inited_t *)Msg;

    // notify pb inited
    MMI_Modem_Phonebook_Init_Ind(MMI_ModemAdp_Get_UI_SimId(CpPbInited->SimId));
}

/**
 * Handler of RIL_MODEMADP_SIM_RSP_GET_PB_STATUS
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Get_Pb_Status_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Get_Pb_Status_t * RilRspGetPbStatus;
    MMI_Modem_Phonebook_Status       * PhkStatus;
    MMI_MODEM_SIM_ID                 SimId;

    RilRspGetPbStatus = (RIL_Modemadp_Rsp_Get_Pb_Status_t *)Msg;
    SimId = MMI_ModemAdp_Get_UI_SimId(RilRspGetPbStatus->SimId);
    printf("%s: simid %d, result %d, total %d, used %d, firstIndex %d\n",
           __FUNCTION__, SimId, RilRspGetPbStatus->result,
           RilRspGetPbStatus->p_phonebook->total, RilRspGetPbStatus->p_phonebook->used,
           RilRspGetPbStatus->p_phonebook->firstIndex);

    PhkStatus = (MMI_Modem_Phonebook_Status *)Hal_Mem_Alloc(sizeof(MMI_Modem_Phonebook_Status));
    if(RIL_E_SUCCESS != RilRspGetPbStatus->result) {
        UI_Log_Out(UI_SUB_MODULE_MODEM_ADP, UI_LOG_LEVEL_ERROR, "Ril request get pb status failed: %d\n", RilRspGetPbStatus->result);
        memset(PhkStatus, 0, sizeof(MMI_Modem_Phonebook_Status));
    }
    else
    {
        g_ModemSimMng.ContactsUsed[SimId] = RilRspGetPbStatus->p_phonebook->used;

        PhkStatus->total      = RilRspGetPbStatus->p_phonebook->total;
        PhkStatus->used       = RilRspGetPbStatus->p_phonebook->used;
        PhkStatus->firstIndex = RilRspGetPbStatus->p_phonebook->firstIndex;
    }
    MMI_Modem_Get_Phonebook_Status_Cnf(SimId, PhkStatus);

    // free the caller memory
    Hal_Mem_Free(RilRspGetPbStatus->p_phonebook);
} /* MMI_ModemAdp_Ril_Sim_Rsp_Get_Pb_Status_Handler */

/**
 * Handler of RIL_MODEMADP_SIM_RSP_WRITE_PB
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Write_Pb_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Write_Pb_t * RilRspWritePb;

    RilRspWritePb = (RIL_Modemadp_Rsp_Write_Pb_t *)Msg;

    // ril returns fail
    if(RIL_E_SUCCESS != RilRspWritePb->result) {
        UI_Log_Out(UI_SUB_MODULE_MODEM_ADP, UI_LOG_LEVEL_ERROR, "Ril request write pb failed: %d\n", RilRspWritePb->result);
        MMI_Modem_Write_Phonebook_Cnf(MMI_ModemAdp_Get_UI_SimId(RilRspWritePb->SimId), MMI_MODEM_CONTACT_INDEX_INVALID);
    } else {
        MMI_Modem_Write_Phonebook_Cnf(MMI_ModemAdp_Get_UI_SimId(RilRspWritePb->SimId), *(RilRspWritePb->p_writtenIndex));
    }
    Hal_Mem_Free(RilRspWritePb->p_writtenIndex);
}

/**
 * change charactor('0' to '9' and 'A' to 'F') to digit(0 to 15)
 * param (in) Str: char
 * return  UINT8
 */
static UINT8 Decode_Char_To_Digit(char Str)
{
    if (('0' <= Str) && ('9' >= Str))
    {
        return(Str - '0');
    }
    else if (('A' <= Str) && ('F' >= Str))
    {
        return(Str - 'A' + 10);
    }
    else
    {
        printf("%s: wrong charactor %d\n", __FUNCTION__, Str);
        return(0);
    }
}

/**
 * Handler of RIL_MODEMADP_SIM_RSP_READ_PB
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Read_Pb_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Read_Pb_t *RilRspReadPb;
    UINT8                      stringLength;
    MMI_MODEM_SIM_ID           SimId;
    RIL_ReadPbIndex            ReadIndex;
    UINT8                      i;
    UINT16                     j;
    UINT16                     NextIndex;
    UINT16                     ReadPbNum;

    RilRspReadPb = (RIL_Modemadp_Rsp_Read_Pb_t *)Msg;
    SimId = MMI_ModemAdp_Get_UI_SimId(RilRspReadPb->SimId);
    printf("%s: simid %d, cnt %d, used %d\n",
           __FUNCTION__, SimId, RilRspReadPb->count, g_ModemSimMng.ContactsUsed[SimId]);

    // ril returns fail
    if(RIL_E_SUCCESS != RilRspReadPb->result) {
        UI_Log_Out(UI_SUB_MODULE_MODEM_ADP, UI_LOG_LEVEL_ERROR, "Ril request get pb status failed: %d\n", RilRspReadPb->result);
        MMI_Modem_Read_Phonebook_Cnf(SimId, NULL, 0, TRUE);
        Hal_Mem_Free(g_ModemSimMng.ReadPbContact[SimId]);
        g_ModemSimMng.ReadPbContact[SimId] = NULL;
        g_ModemSimMng.ContactsUsed[SimId]  = 0;
        return;
    }

    if ((MMI_MODEMADP_SIM_READ_PB_NUM < g_ModemSimMng.ContactsUsed[SimId])
        && (MMI_MODEMADP_SIM_READ_PB_NUM < RilRspReadPb->count)) {
        RilRspReadPb->count = MMI_MODEMADP_SIM_READ_PB_NUM;
    }
    else if (RilRspReadPb->count > g_ModemSimMng.ContactsUsed[SimId]) {
        RilRspReadPb->count = g_ModemSimMng.ContactsUsed[SimId];
    }

    for (j = 0;j < RilRspReadPb->count;j++)
    {
        // store the contact number
        stringLength = strlen(RilRspReadPb->p_contact[j].tel_number);
        memset(g_ModemSimMng.ReadPbContact[SimId][j].Number,
               0, MMI_MODEM_MAX_NUMBER_LEN);
        memcpy(g_ModemSimMng.ReadPbContact[SimId][j].Number,
               RilRspReadPb->p_contact[j].tel_number, stringLength);
        Hal_Mem_Free(RilRspReadPb->p_contact[j].tel_number);

        // store the contact name from SIM, need to set at+cscs="HEX"
        stringLength = strlen(RilRspReadPb->p_contact[j].name);
        INT8 *Name = g_ModemSimMng.ReadPbContact[SimId][j].Name;
        memset(Name, 0, MMI_MODEM_MAX_NAME_LEN);

        if ('8' == RilRspReadPb->p_contact[j].name[0]) /*UTF-8 code*/
        {
            INT8 *ContactName;
            UINT16 Ucs2Str[MMI_MODEM_MAX_NAME_LEN] = {0};
            if ('0' == RilRspReadPb->p_contact[j].name[1])
            {
                stringLength -= 2;
                ContactName = &RilRspReadPb->p_contact[j].name[2];
                i = 0;
                while (4 <= stringLength)
                {
                    Ucs2Str[i] = (Decode_Char_To_Digit(ContactName[i * 4]) << 12)
                        + (Decode_Char_To_Digit(ContactName[i * 4 + 1]) << 8)
                        + (Decode_Char_To_Digit(ContactName[i * 4 + 2]) << 4)
                        + Decode_Char_To_Digit(ContactName[i * 4 + 3]);
                    i++;
                    stringLength -= 4;
                }
                lv_txt_unicode_2_utf8((const UINT16 *)Ucs2Str, i * sizeof(UINT16), Name);
            }
            else if ('1' == RilRspReadPb->p_contact[j].name[1])
            {
                UINT8 CharsLen = (Decode_Char_To_Digit(RilRspReadPb->p_contact[j].name[2]) << 4)
                    + Decode_Char_To_Digit(RilRspReadPb->p_contact[j].name[3]);
                if ((CharsLen * 2 + 6) > stringLength)
                {
                    printf("%s: wrong data %s with 0x81 header\n",
                           __FUNCTION__, RilRspReadPb->p_contact[j].name);
                    return;
                }

                UINT16 BaseAddr = ((Decode_Char_To_Digit(RilRspReadPb->p_contact[j].name[4]) << 4)
                                   + Decode_Char_To_Digit(RilRspReadPb->p_contact[j].name[5])) << 7;
                ContactName = &RilRspReadPb->p_contact[j].name[6];
                UINT8 data;
                for (i = 0;i < CharsLen;i++)
                {
                    data = (Decode_Char_To_Digit(ContactName[i * 2]) << 4)
                        + Decode_Char_To_Digit(ContactName[i * 2 + 1]);
                    if (0 == (data >> 7)) /*ASCII*/
                    {
                        Ucs2Str[i] = data & 0x7F;
                    }
                    else
                    {
                        Ucs2Str[i] = BaseAddr + (data & 0x7F);
                    }
                }
                lv_txt_unicode_2_utf8((const UINT16 *)Ucs2Str, CharsLen * sizeof(UINT16), Name);
            }
            else if ('2' == RilRspReadPb->p_contact[j].name[1])
            {
                UINT8 CharsLen = (Decode_Char_To_Digit(RilRspReadPb->p_contact[j].name[2]) << 4)
                    + Decode_Char_To_Digit(RilRspReadPb->p_contact[j].name[3]);
                if ((CharsLen * 2 + 8) > stringLength)
                {
                    printf("%s: wrong data %s with 0x82 header\n",
                           __FUNCTION__, RilRspReadPb->p_contact[j].name);
                    return;
                }

                UINT16 BaseAddr = (Decode_Char_To_Digit(RilRspReadPb->p_contact[j].name[4]) << 12)
                    + (Decode_Char_To_Digit(RilRspReadPb->p_contact[j].name[5]) << 8)
                    + (Decode_Char_To_Digit(RilRspReadPb->p_contact[j].name[6]) << 4)
                    + Decode_Char_To_Digit(RilRspReadPb->p_contact[j].name[7]);
                ContactName = &RilRspReadPb->p_contact[j].name[8];
                UINT8 data;
                for (i = 0;i < CharsLen;i++)
                {
                    data = (Decode_Char_To_Digit(ContactName[i * 2]) << 4)
                        + Decode_Char_To_Digit(ContactName[i * 2 + 1]);
                    if (0 == (data >> 7)) /*ASCII*/
                    {
                        Ucs2Str[i] = data & 0x7F;
                    }
                    else
                    {
                        Ucs2Str[i] = BaseAddr + (data & 0x7F);
                    }
                }
                lv_txt_unicode_2_utf8((const UINT16 *)Ucs2Str, CharsLen * sizeof(UINT16), Name);
            }
            else
            {
                printf("%s: unknown charactors %d, name %s\n",
                       __FUNCTION__, RilRspReadPb->p_contact[j].name[1],
                       RilRspReadPb->p_contact[j].name);
            }
        }
        else /*ASCII*/
        {
            i = 0;
            while (2 <= stringLength)
            {
                Name[i] = (Decode_Char_To_Digit(RilRspReadPb->p_contact[j].name[i * 2]) << 4)
                    + Decode_Char_To_Digit(RilRspReadPb->p_contact[j].name[i * 2 + 1]);
                stringLength -= 2;
                i++;
            }
        }
        Hal_Mem_Free(RilRspReadPb->p_contact[j].name);

        // store the contact index
        g_ModemSimMng.ReadPbContact[SimId][j].Index = RilRspReadPb->p_contact[j].index;

        NextIndex = RilRspReadPb->p_contact[j].nextindex;
    }
    Hal_Mem_Free(RilRspReadPb->p_contact);

    g_ModemSimMng.ContactsUsed[SimId] -= RilRspReadPb->count;

    // reading not complete
    if(0 < g_ModemSimMng.ContactsUsed[SimId]) {
        MMI_Modem_Read_Phonebook_Cnf(SimId,
                                     g_ModemSimMng.ReadPbContact[SimId],
                                     RilRspReadPb->count,
                                     FALSE);

        // read the next
        if (MMI_MODEMADP_SIM_READ_PB_NUM < g_ModemSimMng.ContactsUsed[SimId]) {
            ReadPbNum = MMI_MODEMADP_SIM_READ_PB_NUM;
        }
        else
        {
            ReadPbNum = g_ModemSimMng.ContactsUsed[SimId];
        }
        // prepare the buffer to store all contacts
        g_ModemSimMng.ReadPbContact[SimId] =
            (MMI_Modem_Contact *)Hal_Mem_Alloc(sizeof(MMI_Modem_Contact) * ReadPbNum);

        // read the contacts
        ReadIndex.firstIndex = NextIndex;
        ReadIndex.endIndex   = ReadIndex.firstIndex + ReadPbNum - 1;
        Ril_SIM_Request_Read_Phonebook(RilRspReadPb->SimId, ReadIndex);
    }
    // all contacts has been read
    else {
        MMI_Modem_Read_Phonebook_Cnf(SimId,
                                     g_ModemSimMng.ReadPbContact[SimId],
                                     RilRspReadPb->count,
                                     TRUE);
        g_ModemSimMng.ReadPbContact[SimId] = NULL;
    }
} /* MMI_ModemAdp_Ril_Sim_Rsp_Read_Pb_Handler */

/**
 * Handler of RIL_MODEMADP_SIM_RSP_GET_SIM_STATUS
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Get_Sim_Status_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Get_Sim_Status_t * RilRspGetSimStatus;
    MMI_Modem_Sim_Status_t            * SimStatus;
    MMI_MODEM_PIN_STATUS_TYPE           PinReqType;
    RIL_AppStatus                     * pRilAppStatus;
    RilRspGetSimStatus = (RIL_Modemadp_Rsp_Get_Sim_Status_t *)Msg;

    printf("%s: SimId=%d, SimOperationStatus=%d\n", __FUNCTION__, RilRspGetSimStatus->SimId, g_ModemSimMng.SimOperationStatus[RilRspGetSimStatus->SimId]);

    if(MODEMADP_SIM_PIN_LOCK_QUERYING == g_ModemSimMng.SimOperationStatus[RilRspGetSimStatus->SimId]) {
        g_ModemSimMng.PinCheckRetryLeft = 0xFF;
        if(RIL_E_SUCCESS == RilRspGetSimStatus->result) {
            pRilAppStatus = &RilRspGetSimStatus->p_cardStatus->applications[RilRspGetSimStatus->p_cardStatus->gsm_umts_subscription_app_index];
            if(MMI_MODEM_PIN1 == g_ModemSimMng.QueryPinLockType) {
                if(RIL_APPSTATE_PIN == pRilAppStatus->app_state) {
                    if((RIL_PINSTATE_DISABLED == pRilAppStatus->pin1) || (RIL_PINSTATE_UNKNOWN == pRilAppStatus->pin1)) {
                        g_ModemSimMng.QueryPinLockResult = FALSE;
                    } else {
                        g_ModemSimMng.QueryPinLockResult = TRUE;
                    }
                } else if(RIL_APPSTATE_PUK == pRilAppStatus->app_state) {
                    // in case PUK required or blocked, though PIN enabled status is unknown, treat as PIN enabled
                    g_ModemSimMng.QueryPinLockResult = TRUE;
                    g_ModemSimMng.PinCheckRetryLeft  = 0;
                } else {
                    // FFS, may give an assert?
                }
            } else if(MMI_MODEM_PIN2 == g_ModemSimMng.QueryPinLockType) {
                if(RIL_APPSTATE_PIN == pRilAppStatus->app_state) {
                    if((RIL_PINSTATE_DISABLED == pRilAppStatus->pin2) || (RIL_PINSTATE_UNKNOWN == pRilAppStatus->pin2)) {
                        g_ModemSimMng.QueryPinLockResult = FALSE;
                    } else {
                        g_ModemSimMng.QueryPinLockResult = TRUE;
                    }
                } else if(RIL_APPSTATE_PUK == pRilAppStatus->app_state) {
                    // in case PUK2 required or blocked, though PIN2 enabled status is unknown, treat as PIN2 enabled
                    g_ModemSimMng.QueryPinLockResult = TRUE;
                    g_ModemSimMng.PinCheckRetryLeft  = 0;
                } else {
                    // FFS, may give an assert?
                }
            } else if(MMI_MODEM_PUK == g_ModemSimMng.QueryPinLockType) {
                if(RIL_APPSTATE_PUK == pRilAppStatus->app_state) {
                    // in case PUK required, though PIN enabled status is unknown, treat as PIN enabled
                    if(RIL_PINSTATE_ENABLED_BLOCKED == pRilAppStatus->pin1) {
                        g_ModemSimMng.QueryPinLockResult = TRUE;
                    }
                    // in case PUK blocked, though PIN enabled status is unknown, treat as PIN enabled
                    else if(RIL_PINSTATE_ENABLED_PERM_BLOCKED == pRilAppStatus->pin1) {
                        g_ModemSimMng.QueryPinLockResult = TRUE;
                        g_ModemSimMng.PinCheckRetryLeft  = 0;
                    }
                } else {
                    // FFS, may give an assert?
                }
            } else if(MMI_MODEM_PUK2 == g_ModemSimMng.QueryPinLockType) {
                if(RIL_APPSTATE_PUK == pRilAppStatus->app_state) {
                    // in case PUK2 required, though PIN2 enabled status is unknown, treat as PIN2 enabled
                    if(RIL_PINSTATE_ENABLED_BLOCKED == pRilAppStatus->pin2) {
                        g_ModemSimMng.QueryPinLockResult = TRUE;
                    }
                    // in case PUK2 blocked, though PIN2 enabled status is unknown, treat as PIN2 enabled
                    else if(RIL_PINSTATE_ENABLED_PERM_BLOCKED == pRilAppStatus->pin2) {
                        g_ModemSimMng.QueryPinLockResult = TRUE;
                        g_ModemSimMng.PinCheckRetryLeft  = 0;
                    }
                } else {
                    // FFS, may give an assert?
                }
            }

            // need further query retried left count
            if(0xFF == g_ModemSimMng.PinCheckRetryLeft) {
                Ril_SIM_Request_Query_Lock_Info(RilRspGetSimStatus->SimId);
            }
            // otherwise, unlock semphore to let UI's pin query request return
            else {
                Hal_Sema_Unlock(g_ModemSimMng.PinCheckSema);
                Hal_Sema_Destroy(g_ModemSimMng.PinCheckSema);

                g_ModemSimMng.SimOperationStatus[RilRspGetSimStatus->SimId] = MODEMADP_SIM_IDLE;
            }
        }
        // otherwise, unlock semphore to let UI's pin query request return
        else {
            // FFS, may give an assert?
            // g_ModemSimMng.PinCheckRetryLeft = ;
            // g_ModemSimMng.QueryPinLockResult = ;
            Hal_Sema_Unlock(g_ModemSimMng.PinCheckSema);
            Hal_Sema_Destroy(g_ModemSimMng.PinCheckSema);

            g_ModemSimMng.SimOperationStatus[RilRspGetSimStatus->SimId] = MODEMADP_SIM_IDLE;
        }
    } else if(MODEMADP_SIM_SIM_PRESENT_CHECKING == g_ModemSimMng.SimOperationStatus[RilRspGetSimStatus->SimId]) {
        if(NULL == g_ModemCallMng.Imei[MMI_ModemAdp_Get_UI_SimId(RilRspGetSimStatus->SimId)]) {
            MMI_Modem_Call_Request_Imei(MMI_ModemAdp_Get_UI_SimId(RilRspGetSimStatus->SimId));
        }
        if(NULL == g_ModemCallMng.BasebandVer) {
            MMI_Modem_Call_Request_Baseband_Version(MMI_ModemAdp_Get_UI_SimId(RilRspGetSimStatus->SimId));
        }
        if(RIL_E_SUCCESS == RilRspGetSimStatus->result) {
            SimStatus = (MMI_Modem_Sim_Status_t *)Hal_Mem_Alloc(sizeof(MMI_Modem_Sim_Status_t));
            Hal_Mem_Set(SimStatus, 0, sizeof(MMI_Modem_Sim_Status_t));
            SimStatus->SimId = MMI_ModemAdp_Get_UI_SimId(RilRspGetSimStatus->SimId);
            if(RIL_CARDSTATE_PRESENT == RilRspGetSimStatus->p_cardStatus->card_state) {
                g_ModemSimMng.SimPresent[RilRspGetSimStatus->SimId] = TRUE;
                SimStatus->SimPresent                               = TRUE;

                printf("%s: app_state=%d\n", __FUNCTION__, RilRspGetSimStatus->p_cardStatus->applications[0].app_state);
                if((NULL == g_ModemSimMng.Imsi[MMI_ModemAdp_Get_UI_SimId(RilRspGetSimStatus->SimId)])
                    && (RIL_APPSTATE_READY == RilRspGetSimStatus->p_cardStatus->applications[0].app_state)) {
                    MMI_Modem_Get_Imsi_Req(MMI_ModemAdp_Get_UI_SimId(RilRspGetSimStatus->SimId));
                }
            } else {
                g_ModemSimMng.SimPresent[RilRspGetSimStatus->SimId] = FALSE;
                SimStatus->SimPresent                               = FALSE;
            }
            MMI_Modem_Sim_Status_Ind(SimStatus);
        } else {
            // FFS, may give an assert?
        }

        g_ModemSimMng.SimOperationStatus[RilRspGetSimStatus->SimId] = MODEMADP_SIM_IDLE;
    } else {
        // ril returns fail
        if(RIL_E_SUCCESS != RilRspGetSimStatus->result) {
            UI_Log_Out(UI_SUB_MODULE_MODEM_ADP, UI_LOG_LEVEL_ERROR, "Ril request get sim status failed: %d\n", RilRspGetSimStatus->result);
            return;
        }

        printf("sim app index %d\n", RilRspGetSimStatus->p_cardStatus->gsm_umts_subscription_app_index);

        // sim status ind
        SimStatus = (MMI_Modem_Sim_Status_t *)Hal_Mem_Alloc(sizeof(MMI_Modem_Sim_Status_t));
        Hal_Mem_Set(SimStatus, 0, sizeof(MMI_Modem_Sim_Status_t));
        SimStatus->SimId = MMI_ModemAdp_Get_UI_SimId(RilRspGetSimStatus->SimId);
        if(RIL_CARDSTATE_PRESENT == RilRspGetSimStatus->p_cardStatus->card_state) {
            SimStatus->SimPresent                               = TRUE;
            g_ModemSimMng.SimPresent[RilRspGetSimStatus->SimId] = TRUE;

            printf("%s: app_state=%d\n", __FUNCTION__, RilRspGetSimStatus->p_cardStatus->applications[0].app_state);
            if((NULL == g_ModemSimMng.Imsi[MMI_ModemAdp_Get_UI_SimId(RilRspGetSimStatus->SimId)])
                && (RIL_APPSTATE_READY == RilRspGetSimStatus->p_cardStatus->applications[0].app_state)) {
                MMI_Modem_Get_Imsi_Req(MMI_ModemAdp_Get_UI_SimId(RilRspGetSimStatus->SimId));
            }
        } else {
            SimStatus->SimPresent                               = FALSE;
            g_ModemSimMng.SimPresent[RilRspGetSimStatus->SimId] = FALSE;
        }
        MMI_Modem_Sim_Status_Ind(SimStatus);

        // pin status ind
        if((0 <= RilRspGetSimStatus->p_cardStatus->gsm_umts_subscription_app_index) &&
                (8 > RilRspGetSimStatus->p_cardStatus->gsm_umts_subscription_app_index)) {
            pRilAppStatus = &RilRspGetSimStatus->p_cardStatus->applications[RilRspGetSimStatus->p_cardStatus->gsm_umts_subscription_app_index];
            if(RIL_APPSTATE_PIN == pRilAppStatus->app_state) {
                if(RIL_PINSTATE_ENABLED_NOT_VERIFIED == pRilAppStatus->pin1) {
                    PinReqType = MMI_MODEM_PIN1_REQUIRED;
                } else if(RIL_PINSTATE_ENABLED_NOT_VERIFIED == pRilAppStatus->pin2) {
                    PinReqType = MMI_MODEM_PIN2_REQUIRED;
                } else {
                    PinReqType = MMI_MODEM_PIN_DISABLED;
                }
            } else if(RIL_APPSTATE_PUK == pRilAppStatus->app_state) {
                if(RIL_PINSTATE_ENABLED_BLOCKED == pRilAppStatus->pin1) {
                    PinReqType = MMI_MODEM_PIN1_BLOCKED_PUK1_REQUIRED;
                } else if(RIL_PINSTATE_ENABLED_BLOCKED == pRilAppStatus->pin2) {
                    PinReqType = MMI_MODEM_PIN2_BLOCKED_PUK2_REQUIRED;
                } else if(RIL_PINSTATE_ENABLED_PERM_BLOCKED == pRilAppStatus->pin1) {
                    PinReqType = MMI_MODEM_PUK1_BLOCKED;
                } else if(RIL_PINSTATE_ENABLED_PERM_BLOCKED == pRilAppStatus->pin2) {
                    PinReqType = MMI_MODEM_PUK2_BLOCKED;
                } else {
                    PinReqType = MMI_MODEM_PIN_DISABLED;
                }
            } else {
                PinReqType = MMI_MODEM_PIN_DISABLED;
            }

            MMI_Modem_Pin_Status_Change_Ind(MMI_ModemAdp_Get_UI_SimId(RilRspGetSimStatus->SimId), PinReqType);
        }
    }

    // free ril memory
    if(NULL != RilRspGetSimStatus->p_cardStatus) {
        Hal_Mem_Free(RilRspGetSimStatus->p_cardStatus);
    }
} /* MMI_ModemAdp_Ril_Sim_Rsp_Get_Sim_Status_Handler */

/**
 * Handler of RIL_MODEMADP_SIM_STATUS_CHANGED
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Sim_Status_Changed_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Sim_Status_Changed_t * SimStatusChanged;

    SimStatusChanged = (RIL_Modemadp_Rsp_Sim_Status_Changed_t *)Msg;

    // get sim status
    Ril_SIM_Request_Get_Sim_Status(SimStatusChanged->SimId);
}

/**
 * Handler of RIL_MODEMADP_SIM_RSP_QUERY_LOCK_INFO
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Query_Lock_Info_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Query_Lock_Info_t * QueryLockInfoRsp;

    QueryLockInfoRsp = (RIL_Modemadp_Rsp_Query_Lock_Info_t *)Msg;

    if(MODEMADP_SIM_PIN_LOCK_QUERYING == g_ModemSimMng.SimOperationStatus[QueryLockInfoRsp->SimId]) {
        // set the retry left variable, then unlock the semphore
        if(RIL_E_SUCCESS == QueryLockInfoRsp->result) {
            if(MMI_MODEM_PIN1 == g_ModemSimMng.QueryPinLockType) {
                g_ModemSimMng.PinCheckRetryLeft = QueryLockInfoRsp->response[0];
            } else if(MMI_MODEM_PIN2 == g_ModemSimMng.QueryPinLockType) {
                g_ModemSimMng.PinCheckRetryLeft = QueryLockInfoRsp->response[1];
            } else if(MMI_MODEM_PUK == g_ModemSimMng.QueryPinLockType) {
                g_ModemSimMng.PinCheckRetryLeft = QueryLockInfoRsp->response[2];
            } else if(MMI_MODEM_PUK2 == g_ModemSimMng.QueryPinLockType) {
                g_ModemSimMng.PinCheckRetryLeft = QueryLockInfoRsp->response[3];
            }
        } else {
            // FFS: may give an assert?
            g_ModemSimMng.PinCheckRetryLeft = 0;
        }
        Hal_Sema_Unlock(g_ModemSimMng.PinCheckSema);

        g_ModemSimMng.SimOperationStatus[QueryLockInfoRsp->SimId] = MODEMADP_SIM_IDLE;
    } else {
        // FFS
    }
    Hal_Mem_Free(QueryLockInfoRsp->response);
} /* MMI_ModemAdp_Ril_Sim_Rsp_Query_Lock_Info_Handler */

/**
 * Handler of RIL_MODEMADP_RSP_SET_FACILITY_LOCK
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Rsp_Set_Facility_Lock_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Set_Facility_Lock_t *SetFacilityLockRsp;

    SetFacilityLockRsp = (RIL_Modemadp_Rsp_Set_Facility_Lock_t *)Msg;

    if(RIL_E_SUCCESS == SetFacilityLockRsp->result)
    {
        g_ModemSimMng.PinCheckRetryLeft = MMI_MODEM_PIN_CHECK_OK;
    }
    else
    {
        g_ModemSimMng.PinCheckRetryLeft = SetFacilityLockRsp->data[0];
    }

    Hal_Sema_Unlock(g_ModemSimMng.PinCheckSema);

    Hal_Mem_Free(SetFacilityLockRsp->data);
}

/**
 * Handler of RIL_MODEMADP_RSP_QUERY_FACILITY_LOCK
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Rsp_Query_Facility_Lock_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Query_Facility_Lock_t *QueryFacilityLockRsp;

    QueryFacilityLockRsp = (RIL_Modemadp_Rsp_Query_Facility_Lock_t *)Msg;

    if(RIL_E_SUCCESS == QueryFacilityLockRsp->result)
    {
        if(1 == QueryFacilityLockRsp->data[0])
        {
            g_ModemSimMng.QueryFacPinLockResult = TRUE;
        }
        else
        {
            g_ModemSimMng.QueryFacPinLockResult = FALSE;
        }
    }
    else
    {
        g_ModemSimMng.QueryFacPinLockResult = FALSE;
    }

    g_ModemSimMng.SimOperationStatus[QueryFacilityLockRsp->SimId]= MODEMADP_SIM_PIN_LOCK_QUERYING;

    Ril_SIM_Request_Query_Lock_Info(QueryFacilityLockRsp->SimId);

    Hal_Mem_Free(QueryFacilityLockRsp->data);
}

/**
 * Handler of RIL_MODEMADP_SIM_RSP_ENTER_SIM_PIN
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Enter_Sim_Pin_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Enter_Sim_Pin_t * EnterSimPin;

    EnterSimPin = (RIL_Modemadp_Rsp_Enter_Sim_Pin_t *)Msg;

    // set the retry left variable, then unlock the semphore
    if(RIL_E_SUCCESS == EnterSimPin->result) {
        g_ModemSimMng.PinCheckRetryLeft = MMI_MODEM_PIN_CHECK_OK;
    } else {
        g_ModemSimMng.PinCheckRetryLeft = *EnterSimPin->p_retryLeft;
    }
    Hal_Sema_Unlock(g_ModemSimMng.PinCheckSema);

    g_ModemSimMng.SimOperationStatus[EnterSimPin->SimId] = MODEMADP_SIM_IDLE;
    Hal_Mem_Free(EnterSimPin->p_retryLeft);
}

/**
 * Handler of RIL_MODEMADP_SIM_RSP_ENTER_SIM_PIN2
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Enter_Sim_Pin2_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Enter_Sim_Pin_t * EnterSimPin2;

    EnterSimPin2 = (RIL_Modemadp_Rsp_Enter_Sim_Pin_t *)Msg;

    // set the retry left variable, then unlock the semphore
    if(RIL_E_SUCCESS == EnterSimPin2->result) {
        g_ModemSimMng.PinCheckRetryLeft = MMI_MODEM_PIN_CHECK_OK;
    } else {
        g_ModemSimMng.PinCheckRetryLeft = *EnterSimPin2->p_retryLeft;
    }
    Hal_Sema_Unlock(g_ModemSimMng.PinCheckSema);
    Hal_Sema_Destroy(g_ModemSimMng.PinCheckSema);

    g_ModemSimMng.SimOperationStatus[EnterSimPin2->SimId] = MODEMADP_SIM_IDLE;
    Hal_Mem_Free(EnterSimPin2->p_retryLeft);
}

/**
 * Handler of RIL_MODEMADP_SIM_RSP_ENTER_SIM_PUK
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Enter_Sim_Puk_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Enter_Sim_Pin_t * EnterSimPuk;

    EnterSimPuk = (RIL_Modemadp_Rsp_Enter_Sim_Pin_t *)Msg;

    // set the retry left variable, then unlock the semphore
    if(RIL_E_SUCCESS == EnterSimPuk->result) {
        g_ModemSimMng.PinCheckRetryLeft = MMI_MODEM_PIN_CHECK_OK;
    } else {
        g_ModemSimMng.PinCheckRetryLeft = *EnterSimPuk->p_retryLeft;
    }
    Hal_Sema_Unlock(g_ModemSimMng.PinCheckSema);
    Hal_Sema_Destroy(g_ModemSimMng.PinCheckSema);

    g_ModemSimMng.SimOperationStatus[EnterSimPuk->SimId] = MODEMADP_SIM_IDLE;
    Hal_Mem_Free(EnterSimPuk->p_retryLeft);
}

/**
 * Handler of RIL_MODEMADP_SIM_RSP_ENTER_SIM_PUK2
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Enter_Sim_Puk2_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Enter_Sim_Pin_t * EnterSimPuk2;

    EnterSimPuk2 = (RIL_Modemadp_Rsp_Enter_Sim_Pin_t *)Msg;

    // set the retry left variable, then unlock the semphore
    if(RIL_E_SUCCESS == EnterSimPuk2->result) {
        g_ModemSimMng.PinCheckRetryLeft = MMI_MODEM_PIN_CHECK_OK;
    } else {
        g_ModemSimMng.PinCheckRetryLeft = *EnterSimPuk2->p_retryLeft;
    }
    Hal_Sema_Unlock(g_ModemSimMng.PinCheckSema);
    Hal_Sema_Destroy(g_ModemSimMng.PinCheckSema);

    g_ModemSimMng.SimOperationStatus[EnterSimPuk2->SimId] = MODEMADP_SIM_IDLE;
    Hal_Mem_Free(EnterSimPuk2->p_retryLeft);
}

/**
 * Handler of RIL_MODEMADP_SIM_RSP_CHG_SIM_PIN
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Change_Sim_Pin_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Enter_Sim_Pin_t * ChgSimPinRsp;

    ChgSimPinRsp = (RIL_Modemadp_Rsp_Enter_Sim_Pin_t *)Msg;

    // set the retry left variable, then unlock the semphore
    if(RIL_E_SUCCESS == ChgSimPinRsp->result) {
        g_ModemSimMng.PinCheckRetryLeft = MMI_MODEM_PIN_CHECK_OK;
    } else {
        g_ModemSimMng.PinCheckRetryLeft = *ChgSimPinRsp->p_retryLeft;
    }
    Hal_Sema_Unlock(g_ModemSimMng.PinCheckSema);

    g_ModemSimMng.SimOperationStatus[ChgSimPinRsp->SimId] = MODEMADP_SIM_IDLE;
    Hal_Mem_Free(ChgSimPinRsp->p_retryLeft);
}

/**
 * Handler of RIL_MODEMADP_SIM_RSP_CHG_SIM_PIN2
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Change_Sim_Pin2_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Enter_Sim_Pin_t * ChgSimPin2Rsp;

    ChgSimPin2Rsp = (RIL_Modemadp_Rsp_Enter_Sim_Pin_t *)Msg;

    // set the retry left variable, then unlock the semphore
    if(RIL_E_SUCCESS == ChgSimPin2Rsp->result) {
        g_ModemSimMng.PinCheckRetryLeft = MMI_MODEM_PIN_CHECK_OK;
    } else {
        g_ModemSimMng.PinCheckRetryLeft = *ChgSimPin2Rsp->p_retryLeft;
    }
    Hal_Sema_Unlock(g_ModemSimMng.PinCheckSema);
    Hal_Sema_Destroy(g_ModemSimMng.PinCheckSema);

    g_ModemSimMng.SimOperationStatus[ChgSimPin2Rsp->SimId] = MODEMADP_SIM_IDLE;
    Hal_Mem_Free(ChgSimPin2Rsp->p_retryLeft);
}

/**
 * Handler of RIL_MODEMADP_SIM_RSP_GET_IMSI
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Get_Imsi_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Get_Imsi_t * GetImsiRsp;

    GetImsiRsp = (RIL_Modemadp_Rsp_Get_Imsi_t *)Msg;

    if(RIL_E_SUCCESS == GetImsiRsp->result) {
        g_ModemSimMng.Imsi[MMI_ModemAdp_Get_UI_SimId(GetImsiRsp->SimId)] = (INT8 *)Hal_Mem_Alloc(strlen(GetImsiRsp->p_imsi) + 1);
        Hal_Mem_Set(g_ModemSimMng.Imsi[MMI_ModemAdp_Get_UI_SimId(GetImsiRsp->SimId)], 0, strlen(GetImsiRsp->p_imsi) + 1);
        Hal_Mem_Copy(g_ModemSimMng.Imsi[MMI_ModemAdp_Get_UI_SimId(GetImsiRsp->SimId)], GetImsiRsp->p_imsi, strlen(GetImsiRsp->p_imsi));
    } else {
        if(NULL != g_ModemSimMng.Imsi[MMI_ModemAdp_Get_UI_SimId(GetImsiRsp->SimId)]) {
            Hal_Mem_Free(g_ModemSimMng.Imsi[MMI_ModemAdp_Get_UI_SimId(GetImsiRsp->SimId)]);
        }
    }

    g_ModemSimMng.SimOperationStatus[GetImsiRsp->SimId] = MODEMADP_SIM_IDLE;

    if(NULL != GetImsiRsp->p_imsi) {
        Hal_Mem_Free(GetImsiRsp->p_imsi);
    }
}

/**
 * Handler of RIL_MODEMADP_SIM_RSP_GET_ICCID
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Get_Iccid_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Get_Iccid_t * GetIccidRsp;

    GetIccidRsp = (RIL_Modemadp_Rsp_Get_Iccid_t *)Msg;

    if(RIL_E_SUCCESS == GetIccidRsp->result) {
        if(NULL != GetIccidRsp->p_iccid) {
            if(g_ModemSimMng.Iccid[MMI_ModemAdp_Get_UI_SimId(GetIccidRsp->SimId)] == NULL) {
                g_ModemSimMng.Iccid[MMI_ModemAdp_Get_UI_SimId(GetIccidRsp->SimId)] = (char *)Hal_Mem_Alloc(strlen(GetIccidRsp->p_iccid) + 1);
            }
            Hal_Mem_Set(g_ModemSimMng.Iccid[MMI_ModemAdp_Get_UI_SimId(GetIccidRsp->SimId)], 0, strlen(GetIccidRsp->p_iccid) + 1);
            Hal_Mem_Copy(g_ModemSimMng.Iccid[MMI_ModemAdp_Get_UI_SimId(GetIccidRsp->SimId)], GetIccidRsp->p_iccid, strlen(GetIccidRsp->p_iccid));
            printf("Iccid is %s in MMI_ModemAdp_Ril_Sim_Rsp_Get_Iccid_Handler\n", g_ModemSimMng.Iccid[MMI_ModemAdp_Get_UI_SimId(GetIccidRsp->SimId)]);
        }
    }

    g_ModemSimMng.SimOperationStatus[GetIccidRsp->SimId] = MODEMADP_SIM_IDLE;

    if(NULL != GetIccidRsp->p_iccid) {
        Hal_Mem_Free(GetIccidRsp->p_iccid);
        GetIccidRsp->p_iccid = NULL;
    }
}

/**
 * Handler of RIL_MODEMADP_SIM_RSP_GET_PLMN
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Get_Plmn_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Get_Plmn_t * GetPlmnRsp;

    GetPlmnRsp = (RIL_Modemadp_Rsp_Get_Plmn_t *)Msg;

    Hal_Mem_Set(g_ModemSimMng.RegHomePlmn[MMI_ModemAdp_Get_UI_SimId(GetPlmnRsp->SimId)].regMnc, 0, 4);
    Hal_Mem_Set(g_ModemSimMng.RegHomePlmn[MMI_ModemAdp_Get_UI_SimId(GetPlmnRsp->SimId)].regMcc, 0, 4);
    Hal_Mem_Set(g_ModemSimMng.RegHomePlmn[MMI_ModemAdp_Get_UI_SimId(GetPlmnRsp->SimId)].homeMnc, 0, 4);
    Hal_Mem_Set(g_ModemSimMng.RegHomePlmn[MMI_ModemAdp_Get_UI_SimId(GetPlmnRsp->SimId)].homeMcc, 0, 4);

    if(RIL_E_SUCCESS == GetPlmnRsp->result) {
        if(NULL != GetPlmnRsp->p_plmn) {
            Hal_Mem_Copy(g_ModemSimMng.RegHomePlmn[MMI_ModemAdp_Get_UI_SimId(GetPlmnRsp->SimId)].regMnc, GetPlmnRsp->p_plmn->regMnc, 4);
            Hal_Mem_Copy(g_ModemSimMng.RegHomePlmn[MMI_ModemAdp_Get_UI_SimId(GetPlmnRsp->SimId)].regMcc, GetPlmnRsp->p_plmn->regMcc, 4);
            Hal_Mem_Copy(g_ModemSimMng.RegHomePlmn[MMI_ModemAdp_Get_UI_SimId(GetPlmnRsp->SimId)].homeMnc, GetPlmnRsp->p_plmn->homeMnc, 4);
            Hal_Mem_Copy(g_ModemSimMng.RegHomePlmn[MMI_ModemAdp_Get_UI_SimId(GetPlmnRsp->SimId)].homeMcc, GetPlmnRsp->p_plmn->homeMcc, 4);
            printf("regMnc is %s in MMI_ModemAdp_Ril_Sim_Rsp_Get_Plmn_Handler\n", g_ModemSimMng.RegHomePlmn[MMI_ModemAdp_Get_UI_SimId(GetPlmnRsp->SimId)].regMnc);
            printf("regMcc is %s in MMI_ModemAdp_Ril_Sim_Rsp_Get_Plmn_Handler\n", g_ModemSimMng.RegHomePlmn[MMI_ModemAdp_Get_UI_SimId(GetPlmnRsp->SimId)].regMcc);
            printf("homeMnc is %s in MMI_ModemAdp_Ril_Sim_Rsp_Get_Plmn_Handler\n", g_ModemSimMng.RegHomePlmn[MMI_ModemAdp_Get_UI_SimId(GetPlmnRsp->SimId)].homeMnc);
            printf("homeMcc is %s in MMI_ModemAdp_Ril_Sim_Rsp_Get_Plmn_Handler\n", g_ModemSimMng.RegHomePlmn[MMI_ModemAdp_Get_UI_SimId(GetPlmnRsp->SimId)].homeMcc);
            printf("%s: sim %d, homeplmn %s, regplmn %s\n",
                   __FUNCTION__, GetPlmnRsp->SimId, GetPlmnRsp->p_plmn->homePlmn, GetPlmnRsp->p_plmn->regPlmn);
            INT8 * Operator = NULL;
            INT8 * OperTemp = GetPlmnRsp->p_plmn->regPlmn;
            UINT8 OperLen = strlen(GetPlmnRsp->p_plmn->regPlmn);
            if(0 == OperLen) {
                OperLen = strlen(GetPlmnRsp->p_plmn->homePlmn);
                OperTemp = GetPlmnRsp->p_plmn->homePlmn;
            }
            if(0 < OperLen) {
                Operator = (INT8 *)Hal_Mem_Alloc(OperLen + 1);
                Hal_Mem_Copy(Operator, OperTemp, OperLen + 1);
            }
            MMI_Modem_Operator_Ind(MMI_ModemAdp_Get_UI_SimId(GetPlmnRsp->SimId), Operator, MMI_MODEM_PLMN_RAT_UNKNOW);
        }
    }

    g_ModemSimMng.SimOperationStatus[GetPlmnRsp->SimId] = MODEMADP_SIM_IDLE;

    if(NULL != GetPlmnRsp->p_plmn) {
        Hal_Mem_Free(GetPlmnRsp->p_plmn);
        GetPlmnRsp->p_plmn = NULL;
    }
}

/**
 * Handler of RIL_MODEMADP_SIM_RSP_GET_MSISDN
 * param (in) Msg: UI_Inter_Msg_t*
 * return  void
 */
VOID MMI_ModemAdp_Ril_Sim_Rsp_Get_Msisdn_Handler(UI_Inter_Msg_t * Msg)
{
    RIL_Modemadp_Rsp_Get_Msisdn_t * GetMsisdnRsp;

    GetMsisdnRsp = (RIL_Modemadp_Rsp_Get_Msisdn_t *)Msg;

    if(RIL_E_SUCCESS == GetMsisdnRsp->result) {
        if(NULL != GetMsisdnRsp->p_msisdn) {
            g_ModemSimMng.Msisdn[MMI_ModemAdp_Get_UI_SimId(GetMsisdnRsp->SimId)] = (char *)Hal_Mem_Alloc(strlen(GetMsisdnRsp->p_msisdn) + 1);
            Hal_Mem_Set(g_ModemSimMng.Msisdn[MMI_ModemAdp_Get_UI_SimId(GetMsisdnRsp->SimId)], 0, strlen(GetMsisdnRsp->p_msisdn) + 1);
            Hal_Mem_Copy(g_ModemSimMng.Msisdn[MMI_ModemAdp_Get_UI_SimId(GetMsisdnRsp->SimId)], GetMsisdnRsp->p_msisdn, strlen(GetMsisdnRsp->p_msisdn));
            printf("Msisdn is %s in MMI_ModemAdp_Ril_Sim_Rsp_Get_Msisdn_Handler\n", g_ModemSimMng.Msisdn[MMI_ModemAdp_Get_UI_SimId(GetMsisdnRsp->SimId)]);
        }
    }

    g_ModemSimMng.SimOperationStatus[GetMsisdnRsp->SimId] = MODEMADP_SIM_IDLE;

    if(NULL != GetMsisdnRsp->p_msisdn) {
        Hal_Mem_Free(GetMsisdnRsp->p_msisdn);
        GetMsisdnRsp->p_msisdn = NULL;
    }
}

/**
 * module event handler
 * param (in) void
 * return  void
 */
VOID MMI_ModemAdp_Sim_Msg_handle(UI_INTER_MSG_ID MsgId, UI_Inter_Msg_t * pMsg)
{
    switch(MsgId) {
        case MMI_MODEMADP_ENABLE_PIN_LOCK:
            MMI_ModemAdp_Enable_Pin_Lock_Handler(pMsg);
            break;

        case MMI_MODEMADP_QUERY_FACILITY_PIN_LOCK:
            MMI_ModemAdp_Query_Facility_Pin_Lock_Handler(pMsg);
            break;

        case MMI_MODEMADP_QUERY_PIN_LOCK:
            MMI_ModemAdp_Query_Pin_Lock_Handler(pMsg);
            break;

        case MMI_MODEMADP_TRY_PIN_REQ:
            MMI_ModemAdp_Try_Pin_Req_Handler(pMsg);
            break;

        case MMI_MODEMADP_TRY_PIN2_REQ:
            MMI_ModemAdp_Try_Pin2_Req_Handler(pMsg);
            break;

        case MMI_MODEMADP_TRY_PUK_REQ:
            MMI_ModemAdp_Try_Puk_Req_Handler(pMsg);
            break;

        case MMI_MODEMADP_TRY_PUK2_REQ:
            MMI_ModemAdp_Try_Puk2_Req_Handler(pMsg);
            break;

        case MMI_MODEMADP_CHG_PIN_REQ:
            MMI_ModemAdp_Change_Pin_Req_Handler(pMsg);
            break;

        case MMI_MODEMADP_CHG_PIN2_REQ:
            MMI_ModemAdp_Change_Pin2_Req_Handler(pMsg);
            break;

        case MMI_MODEMADP_SIM_PRESENT_CHK_REQ:
            MMI_ModemAdp_Sim_Present_Check_Req_Handler(pMsg);
            break;

        case MMI_MODEMADP_GET_PB_STATUS_REQ:
            MMI_ModemAdp_Get_Pb_Status_Req_Handler(pMsg);
            break;

        case MMI_MODEMADP_WRITE_PB_REQ:
            MMI_ModemAdp_Write_Pb_Req_Handler(pMsg);
            break;

        case MMI_MODEMADP_READ_PB_REQ:
            MMI_ModemAdp_Read_Pb_Req_Handler(pMsg);
            break;

        case MMI_MODEMADP_GET_IMSI_REQ:
            MMI_ModemAdp_Get_Imsi_Req_Handler(pMsg);
            break;

        case MMI_MODEMADP_GET_ICCID_REQ:
            MMI_ModemAdp_Get_Iccid_Req_Handler(pMsg);
            break;

        case MMI_MODEMADP_GET_PLMN_REQ:
            MMI_ModemAdp_Get_Plmn_Req_Handler(pMsg);
            break;

        case MMI_MODEMADP_GET_MSISDN_REQ:
            MMI_ModemAdp_Get_Msisdn_Req_Handler(pMsg);
            break;

        case MMI_MODEMADP_SEL_CHAR_SET_REQ:
            MMI_ModemAdp_Sel_Char_Set_Req_Handler(pMsg);
            break;

        case MMI_MODEMADP_SIM_SOFT_RESET_REQ:
            MMI_ModemAdp_Sim_Soft_Reset_Req_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_CP_PB_INITED:
            MMI_ModemAdp_Ril_Sim_Rsp_Cp_Pb_Inited_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_GET_PB_STATUS:
            MMI_ModemAdp_Ril_Sim_Rsp_Get_Pb_Status_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_WRITE_PB:
            MMI_ModemAdp_Ril_Sim_Rsp_Write_Pb_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_READ_PB:
            MMI_ModemAdp_Ril_Sim_Rsp_Read_Pb_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_STATUS_CHANGED:
            MMI_ModemAdp_Ril_Sim_Rsp_Sim_Status_Changed_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_GET_SIM_STATUS:
            MMI_ModemAdp_Ril_Sim_Rsp_Get_Sim_Status_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_QUERY_LOCK_INFO:
            MMI_ModemAdp_Ril_Sim_Rsp_Query_Lock_Info_Handler(pMsg);
            break;

        case RIL_MODEMADP_RSP_SET_FACILITY_LOCK:
            MMI_ModemAdp_Ril_Rsp_Set_Facility_Lock_Handler(pMsg);
            break;

        case RIL_MODEMADP_RSP_QUERY_FACILITY_LOCK:
            MMI_ModemAdp_Ril_Rsp_Query_Facility_Lock_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_ENTER_SIM_PIN:
            MMI_ModemAdp_Ril_Sim_Rsp_Enter_Sim_Pin_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_ENTER_SIM_PIN2:
            MMI_ModemAdp_Ril_Sim_Rsp_Enter_Sim_Pin2_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_ENTER_SIM_PUK:
            MMI_ModemAdp_Ril_Sim_Rsp_Enter_Sim_Puk_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_ENTER_SIM_PUK2:
            MMI_ModemAdp_Ril_Sim_Rsp_Enter_Sim_Puk2_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_CHG_SIM_PIN:
            MMI_ModemAdp_Ril_Sim_Rsp_Change_Sim_Pin_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_CHG_SIM_PIN2:
            MMI_ModemAdp_Ril_Sim_Rsp_Change_Sim_Pin2_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_GET_IMSI:
            MMI_ModemAdp_Ril_Sim_Rsp_Get_Imsi_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_GET_ICCID:
            MMI_ModemAdp_Ril_Sim_Rsp_Get_Iccid_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_GET_PLMN:
            MMI_ModemAdp_Ril_Sim_Rsp_Get_Plmn_Handler(pMsg);
            break;

        case RIL_MODEMADP_SIM_RSP_GET_MSISDN:
            MMI_ModemAdp_Ril_Sim_Rsp_Get_Msisdn_Handler(pMsg);
            break;
        default:
            break;
    } /* switch */
} /* MMI_ModemAdp_Sim_Event_handle */

#endif // USE_LV_WATCH_MODEM_ADAPTOR
